gpt4 book ai didi

Java SWT : removeAll() in TABLE doesn't delete my buttons on the rows

转载 作者:行者123 更新时间:2023-11-30 10:11:26 24 4
gpt4 key购买 nike

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

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

这是我的 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");

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

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

关于Java SWT : removeAll() in TABLE doesn't delete my buttons on the rows,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52440051/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com