在桌面上为Eclipse向导工作 - 删除所有项目后,自定义小部件仍然存在

时间:2014-03-04 10:46:53

标签: java eclipse swt jface

请注意Eclipse向导的以下Java代码:

public class PageTypes extends WizardPage
{

    private Table types;

    @Override
    public void setVisible(boolean visible)
    {
        // if we get visible, we want to update the tables!
        if(visible)
            initialize();
        super.setVisible(visible);
    }

    private void initialize()
    {
        types.setRedraw(false);
        types.removeAll();
        types.setRedraw(true);
        ...
        //getting data from database and start a cycle for adding rows
        ...
        //adding data for cells in first two columns: plain text - no problems here
        ...
        TableEditor editor = new TableEditor (types);
        Button checkButton = new Button(types, SWT.CHECK);
        checkButton.setSelection(true);
        checkButton.pack();
        editor.minimumWidth = checkButton.getSize ().x;
        editor.horizontalAlignment = SWT.CENTER;
        editor.setEditor(checkButton, item, 2);
        ...
        //here comes another widget (comboBox) for the fourth column
        ...
    }
}

到目前为止,我只调用此页面一次。如果我返回然后再转发,removeAll()删除所有纯文本条目,但不对checkButtons和comboBoxes做任何事情。所以当它重绘表时,我会看到两次元素。当我在页面之间切换时选择另一个数据源时,看起来更奇怪。这是一个截图,使我的问题更加清晰:

screenshot

有什么建议吗?

1 个答案:

答案 0 :(得分:0)

我想出了如何解决我的问题。我需要保存对小部件的每个编辑器的引用,然后在删除项目之前处置它们。这是一个片段:

//this goes in the cycle where widgets are created
item.setData("checkButtonEditor", editor);
//here comes another widget (comboBox) for the fourth column
item.setData("checkBoxEditor", editor);

//this goes into the beggining of initialze-method
types.setRedraw(false);
TableItem[] items = types.getItems();
for (TableItem item : items)
{
    TableEditor editor = (TableEditor)item.getData("checkButtonEditor");
    editor.getEditor().dispose();
    editor.dispose();
    editor = (TableEditor)item.getData("checkBoxEditor");
    editor.getEditor().dispose();
    editor.dispose();
}
types.removeAll();
types.setRedraw(true);

感谢来自dev.eclipse.org

的Carolyn MacLeod