如何处置Composite对象的所有子对象? [英] How to dispose all children of Composite object?

查看:90
本文介绍了如何处置Composite对象的所有子对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个

Composite descComp

里面有一些东西...基本上,它是一种表单的容器,由标签,组合和按钮的数量组成,它们全部排成一行。
我的表单不是有限的,我有一个按钮,可以添加一行额外的行来进行额外的输入。但是,要使它正常工作,我必须处置descComp的大孩子...

with some stuff in it... basically it is a container for a form, consisting of number of labels, Combos and buttons, all aligned in a line. My form is not finite, I have a button that adds one extra line for extra input. However for that to work it seams I have to dispose old children of my descComp...

private void populateConstantMain(ContentData tariffConstantsOfType, Composite descComp,GridLayout descCompLayout, Boolean resize) {

    int arraySize;

    if (resize == false) {
        arraySize = tariffConstantsOfType.getQueryRowCount();
    } else {
        for (int i = 0 ; i < descComp.getChildren().length; i++) {
            System.out.println("Disposing: " + i + " " + descComp.getChildren()[i].toString());
            //descComp.getChildren()[i].setEnabled(false);
            descComp.getChildren()[i].dispose();
        }
        arraySize = tariffConstantsOfType.getQueryRowCount() + 1;
    }
......
}

出于某些原因

descComp.getChildren()[i].dispose();

不起作用,这意味着它不会处置所有子项,从而导致在插入新子项时出错,因此破坏布局:/有趣的是

does not work, meaning it wont dispose all children, that results in errors in inserting new children, therefore spoiling the layout :/ Interesting thing is that

descComp.getChildren()[i].setEnabled(false);

在我不参加时,它适用于所有孩子...

works, when I uncoment it, for all children...

推荐答案

我有一种直觉,即在复合对象上调用getChildren()只会在您调用它时返回未处置的子对象。因此,随着索引的增加而数组的大小减小,调用 descComp.getChildren()[i] .dispose(); 都一团糟。您为什么不尝试:

I have a hunch that calling getChildren() on a composite returns you only the non-disposed children at the time you call it. So calling descComp.getChildren()[i].dispose(); is all messed up as your index is incrementing but your array is decreasing in size. Why don't you try:

    for (Control control : descComp.getChildren()) {
        control.dispose();
    }

通过这种方式,您可以在开始之前在组合中获得子视图的静态视图处理它们中的每一个。

This way you get a static view of the children in the composite before you start disposing of each of them.

我还切换了代码,以使用更好的J5 for-each语法。如果您坚持使用J1.4,那么不幸的是,您需要坚持使用 for(;;)循环:

I've also switched the code to use the nicer J5 for-each syntax. If you are stuck on J1.4 then unfortunately you'll need to stick to the for(;;) loop:

    Control[] children = descComp.getChildren();
    for (int i = 0 ; i < children.length; i++) {
        children[i].dispose();
    }

这篇关于如何处置Composite对象的所有子对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆