java 如何处理 Composite 对象的所有子级?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10762415/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to dispose all children of Composite object?
提问by mentis
I hava a
我有一个
Composite 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...
里面有一些东西......基本上它是一个表单的容器,由许多标签、组合和按钮组成,所有这些都排成一行。我的表单不是有限的,我有一个按钮可以为额外的输入添加一行。然而,为了让它工作,我必须处理我的 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;
}
......
}
For some reason
由于某些原因
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...
对所有孩子都有效,当我取消注释时......
回答by Malcolm Smith
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:
我有一种预感,在组合上调用 getChildren() 只会在您调用它时返回未处置的孩子。因此descComp.getChildren()[i].dispose();
,当您的索引增加但数组的大小却在减少时,调用就变得一团糟。为什么不试试:
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.
通过这种方式,您可以在开始处理每个子项之前获得复合中子项的静态视图。
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:
我还切换了代码以使用更好的 J5 for-each 语法。如果您被困在 J1.4 上,那么不幸的是,您将需要坚持for(;;)
循环:
Control[] children = descComp.getChildren();
for (int i = 0 ; i < children.length; i++) {
children[i].dispose();
}
回答by mike
When disposing children (or anything in an array) I use the for next loop but go through from end to start, not start to end. (And get the length before the loop or it changes.)
在处理子项(或数组中的任何内容)时,我使用 for next 循环,但从头到尾进行,而不是从头到尾。(并在循环之前获取长度或更改。)
int i = length-1;i>=0;i--
otherwise you delete every other one.
否则你删除所有其他的。