Working on Table for a Eclipse Wizard - Custom widgets remain after removing all items

StackOverflow https://stackoverflow.com/questions/22169767

  •  02-06-2023
  •  | 
  •  

문제

Regard the following Java code for an Eclipse Wizard:

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
        ...
    }
}

This works so far when i call this page only once. If i go back and then forward again, removeAll() removes all plain text entries, but doesn't do anything with checkButtons and comboBoxes. So when it redraws the table, i see elements twice. It looks more weird when i select another data source while switching between pages. Here's a screenshot to make my problem more clear:

screenshot

Any suggestions?

도움이 되었습니까?

해결책

I figured out how to solve my problem. I needed to save reference to every single editor for the widgets and then dispose them before removing items. Here's a snippet:

//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);

Thanks to Carolyn MacLeod from dev.eclipse.org

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top