Java SWT Tree TreeItem Listener返回Widget Disposed Exception Error

时间:2016-08-26 13:51:49

标签: java tree widget swt dispose

我制作了一个程序,通过文本框和按钮以及文件对话框从用户那里获取几个输入,并将它们格式化为SWT树,用户可以通过复选框选择它的元素。

我面临的问题是我希望它能够为用户实时更新,并且我在此论坛中找到了一个关于使用容器layout()的解决方案方法,它适用于我的树,但只有当我在这棵树上调用dispose(),然后重建它/之后重新绘制它。

在这棵树后面,我有一个Tree Data Structure,用于管理所有数据。 此外,I类使用实现SWT' Dialog接口,因此当我按下按钮时,将显示一个带有树和文本输入区域的窗口。

我已经在类中声明了树所在的容器。

final Composite container = new Composite(parent, SWT.NONE);

接下来我调用这个包含树的实际构建过程的静态方法

createTree(container);

我有一些用于用户输入的按钮和文本区域的其他代码,但这不会影响程序的行为或抛出的异常。

这是静态方法createTree(container);

public void createTree(final Composite container){
    try{
        for(Object element : container.getChildren()){
            if(element instanceof Tree){
                ((Tree) element).dispose();
            }
        }//here I am disposing of the previous tree and then I'm creating a new one to be drawn in the container when the layout() method will be called

        final Tree variantTree = new Tree(container, SWT.CHECK | SWT.V_SCROLL | SWT.H_SCROLL);
        variantTree.setBounds(10, 65, 400, 400);

//here is where I am populating the tree with the data I have stored in the Tree Data Structure that I've mentioned

        if(TestControl.getTree().getChildren().size() > 0){
            for(final Node variantElement : TestControl.getTree().getChildren()){
                final TreeItem variantTreeItem = new TreeItem(variantTree, 0);
                variantTreeItem.setText(variantElement.getName());
                variantTreeItem.setChecked(variantElement.getState());

                for(Node suiteElement : variantElement.getChildren()){
                    TreeItem suiteTreeItem = new TreeItem(variantTreeItem, 0);
                    suiteTreeItem.setText(suiteElement.getName());
                    suiteTreeItem.setChecked(suiteElement.getState());

                    for(Node testElement : suiteElement.getChildren()){
                        TreeItem testTreeItem = new TreeItem(suiteTreeItem, 0);
                        testTreeItem.setText(testElement.getName());
                        testTreeItem.setChecked(testElement.getState());
                    }
                }
            }
        }
        //here is the actual problem, the exception's stack trace points to the line where my next comment is. this listener is used to bring a file dialog window where I can select a file and use it later on 

        variantTree.addListener(SWT.MouseDoubleClick, new Listener(){
            @Override
            public void handleEvent(Event event) {
                try{
                    // TODO Auto-generated method stub
                    Point point = new Point(event.x, event.y);
                    if(!point.equals(null)){
                        TreeItem item = variantTree.getItem(point);
                        for(Node element : TestControl.getTree().getChildren()){
                            if(element.getName().equals(item.getText())){//here is the problem, why is it trying to tell me 

                                FileDialog fileDialog = new FileDialog(container.getParent().getShell(), SWT.OPEN);
                                //filtering for extensions
                                //filtering for path

                                String path;

                                if((path = fileDialog.open()) != null){
                                    String fileName = fileDialog.getFileName();
                                    Node suiteNode =  new Node(element);
                                    suiteNode.setName(fileName);
                                    TestControl.addChild(suiteNode);


                                    createTree(container);
//here I call the method in a recursive way. After I modified my Tree Data Structure with the data I got from the user, I want to redraw the tree in a real timed fashion

                                }
                            }
                        }   
                    }
                }catch(Exception exception){
                    exception.printStackTrace();
                    Status status = new Status(IStatus.ERROR, Activator.PLUGIN_ID,  exception.getLocalizedMessage(), exception);
                    ErrorDialog.openError(null, "Error", "Error occured!", status);
                }
            }
        });

        variantTree.addListener(SWT.Selection, new Listener(){

            @Override
            public void handleEvent(Event event) {
                // TODO Auto-generated method stub
                if(event.detail == SWT.CHECK){
                    TreeItem item = (TreeItem) event.item;

                    for(Node element : TestControl.getTree().getChildren()){
                        if(element.getName().equals(item.getText())){
                            element.setState(item.getChecked());
                        }
                    }

                    for(Node element : TestControl.getTree().getChildren()){
                        for(Node nextElement : element.getChildren()){
                            if(nextElement.getName().equals(item.getText())){//here the error doesnt show up, even though I am using the SWT Tree element as above
                                nextElement.setState(item.getChecked());
                            }
                        }
                    }

                    for(Node element : TestControl.getTree().getChildren()){
                        for(Node nextElement : element.getChildren()){
                            for(Node lastElement : nextElement.getChildren()){
                                if(lastElement.getName().equals(item.getText())){
                                    lastElement.setState(item.getChecked());
                                }
                            }
                        }
                    }
                }
            }
        });
    }catch(Exception exception){
        exception.printStackTrace();
        Status status = new Status(IStatus.ERROR, Activator.PLUGIN_ID,  exception.getLocalizedMessage(), exception);
        ErrorDialog.openError(null, "Error", "Error occured!", status);
    }
}

我也读过这个错误可能会出现,因为当我调用dispose时,我也应该摆脱听众。 是否有必要,这可能是例外的来源? 谢谢,抱歉拥抱代码部分。

1 个答案:

答案 0 :(得分:1)

我认为在显示createTree后调用FileDialog方法时会出现错误。

当您呼叫createTree时,您正处于此循环中:

for(Node element : TestControl.getTree().getChildren()){

createTree调用之后立即返回循环开始并运行

if(element.getName().equals(item.getText()))

但此处item指的是您刚刚处置的TreeItem中的Tree,因此它已不再有效,您可以将“小部件”放置在'错误。

调用createTree后,必须立即停止循环,不要在现有树上执行任何其他操作。一个break来阻止循环就足够了:

       createTree(container);
       break;

注意:您不应该处置Tree,只需删除TreeItem并添加新的就足够了。