Java SWT:TABLE中的removeAll()不会删除行上的按钮

时间:2018-09-21 08:53:38

标签: java eclipse swt windowbuilder

我有一个包含4列的表格,第四个填充有CHECK样式的按钮。

问题在于,每次我执行removeAll()来用其他内容填充表格时,按钮都不会被删除,因此我可以在同一行中看到具有相同状态的相同按钮。

这是我的桌子:

Table membersTable = new Table(clubComposite, SWT.BORDER | SWT.CHECK | SWT.FULL_SELECTION);
membersTable.setLinesVisible(true);
membersTable.setHeaderVisible(true);
membersTable.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));

TableColumn tblclmnName = new TableColumn(membersTable, SWT.NONE);
tblclmnName.setWidth(150);
tblclmnName.setText("Nombre");

TableColumn tblclmnCommonPhoneNumber = new TableColumn(membersTable, SWT.NONE);
tblclmnCommonPhoneNumber.setWidth(120);
tblclmnCommonPhoneNumber.setText("Teléfono");

TableColumn tblclmnCommonMoney = new TableColumn(membersTable, SWT.NONE);
tblclmnCommonMoney.setWidth(150);
tblclmnCommonMoney.setText("Participación Habitual");

TableColumn tblclmnPayed = new TableColumn(membersTable, SWT.CENTER);
tblclmnPayed.setWidth(50);
tblclmnPayed.setText("Payed"); 

这是我填充表格的方式,您也可以看到按钮:

// populate Table
for (int i=0; i<50; i++) {
    TableItem tableItem = new TableItem(membersTable, SWT.NONE);                    
    tableItem.setText(new String[] {"person "+i, "610610620", "100"});

    Button button = new Button(membersTable, SWT.CHECK);
    button.pack();
    TableEditor editor = new TableEditor(membersTable);
    editor.minimumWidth = button.getSize().x;
    editor.horizontalAlignment = SWT.CENTER;
    editor.setEditor(button, tableItem, 3);

    button.addSelectionListener(new SelectionAdapter() {
        @Override
        public void widgetSelected(SelectionEvent e) {
            //how to know in which row is clicked the checkbox?
        }
    });
}

1 个答案:

答案 0 :(得分:1)

TableEditor不是TableItem,不会被removeAll删除(也不会被clearAll删除)。

相应的控件可以通过编程方式放置,例如:

membersTable.removeAll();
Arrays.stream(membersTable.getChildren())    // stream over all children
      .filter(Button.class::isInstance)      // only the Buttons
      .forEach(Control::dispose);            // and dispose it

或(与上述相同)

membersTable.removeAll();
for(Control control : membersTable.getChildren()) {
    if (control instanceof Button) {
        control.dispose();
    }
}

或者,将TableEditor保存在列表中,并处置其编辑器:

List<TableEditor> editors = new ArrayList<>();
...
membersTable.removeAll();
editors.stream()                      // stream over TableEditors
       .map(TableEditor::getEditor)   // get their Editor (a Control)
       .forEach(Control::dispose);    // dispose it
editors.clear();