Java SWT:TABLE 中的 removeAll() 不会删除我在行上的按钮
Java SWT: removeAll() in TABLE doesn't delete my buttons on the rows
我有一个 Table 有 4 列,第四列是 CHECK 样式的按钮。
问题是每次我执行 removeAll() 以用其他内容填充 table 时,按钮都没有被删除,所以我可以在相同的位置看到具有相同状态的相同按钮行。
这是我的 table:
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");
这就是我填充 table 的方式,您还可以看到按钮:
// 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?
}
});
}
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();
我有一个 Table 有 4 列,第四列是 CHECK 样式的按钮。
问题是每次我执行 removeAll() 以用其他内容填充 table 时,按钮都没有被删除,所以我可以在相同的位置看到具有相同状态的相同按钮行。
这是我的 table:
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");
这就是我填充 table 的方式,您还可以看到按钮:
// 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?
}
});
}
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();