I have a simple SWT application which contains menus and controls. I have Text, tables, and trees available in the application, and I need to explicitly call the dispose method to clear the current object and give space for displaying other widgets on the screen. The system crashes when I call the dispose method if the widget is not already activated. Is there any better approach available to dispose an active widget to give room for another widget?
问问题
1461 次
1 回答
2
如果您确实需要处理小部件,您可以保留已添加项目的列表。仅处置已添加到列表中的项目。
所以我会用我的主容器类覆盖 JFrame,并有一两个方法将控件添加到列表中。(如果您需要对它们的去向进行特殊控制,您也可以在添加控制方法中传递一个选项类)
class MainContainer extends JFrame {
private List<JComponent> currentComponents = new ArrayList<JComponent>();
public void addControl(JComponent newComp) {
// -- add it to the JFrame --
...
// -- make a note that it is on --
currentComponents.add(newComp);
}
public void removeControl(JComponent oldComp) {
// -- check if it is in the list --
if (currentComponents.contains(oldComp)) {
// -- remove it from the JFrame --
...
// -- remove it from the list --
currentComponents.remove(oldComp);
}
}
}
或者,您的所有组件都可以被覆盖并显示一个标志
private boolean isDisplayed();
方法和私有 setDisplayed(boolean state); 这样,当您进行添加时,您将显示状态设置为 true,而当您进行删除时,只有在显示状态为 true 时才执行此操作,然后将状态设置为 false。
于 2012-05-01T14:21:53.930 回答