有没有办法从表中删除所有 按钮单元 或确定表中的索引?所有行都有删除按钮,每行完美地工作,但现在我需要实现删除所有方法并陷入这些按钮单元问题。执行table.removeAll()
时,除了Button
或TableEditor
以外,所有数据都会受到影响。以下是我正在处理它的代码和显示发生情况的图像:
table.remove(index)
或table.RemoveAll()
方法无法生成按钮,因此我使用dispose()
删除每行的按钮。 获取每个按钮的行索引的任何解决方案都会让我更感激 。
注意:在我尝试实施删除方法时添加了注释。
public class Test implements Listener {
private Composite composite;
private Button button;
Alarm alarm;
private TableViewer tableViewer;
private Table table;
private Button operationButtons;
private Button removeButtons;
private Button checkButtons;
private TabFolder tabFolder;
private String name;
RestClientAlarmRegions alarms = new RestClientAlarmRegions();
final Point newSize;
public Test(TabFolder tabFolder) {
this.tabFolder = tabFolder;
this.name = "Test";
TabItem item = new TabItem(this.tabFolder, SWT.NONE);
item.setText(this.name);
item.setToolTipText(this.name);
final int insetX = 4, insetY = 4;
FormLayout formLayout = new FormLayout();
formLayout.marginWidth = insetX;
formLayout.marginHeight = insetY;
composite = new Composite(tabFolder, SWT.NONE);
newSize = composite.computeSize(SWT.DEFAULT, SWT.DEFAULT, true);
composite.setLayout(formLayout);
Label label = new Label(composite, SWT.WRAP);
label.setText("List");
button = new Button(composite, SWT.NONE);
button.setText("Remove All");
button.setSize(new Point(100, 40));
FormData buttonData = new FormData();
buttonData.right = new FormAttachment(100, -insetX);
buttonData.bottom = new FormAttachment(100, 0);
button.setLayoutData(buttonData);
button.setVisible(true);
button.addListener(SWT.Selection, new Listener() {
@Override
public void handleEvent(Event e) {
if (e.widget instanceof Button) {
// how to determine index of this buttons or editors to
// remove in any loop..? also created a for loop for
// disposing all button without index but effected only one
// row's buttons.
removeButtons.dispose();
checkButtons.dispose();
operationButtons.dispose();
table.removeAll();
}
}
});// remove all button listener that works for only row data not
// buttons.
tableViewer = new TableViewer(composite, SWT.BORDER);
table = tableViewer.getTable();
TableViewerColumn tableColumn1 = new TableViewerColumn(tableViewer,
SWT.CENTER);
TableViewerColumn tableColumn2 = new TableViewerColumn(tableViewer,
SWT.CENTER);
TableViewerColumn tableColumn7 = new TableViewerColumn(tableViewer,
SWT.CENTER);
TableViewerColumn tableColumn8 = new TableViewerColumn(tableViewer,
SWT.CENTER);
tableColumn1.getColumn().setText("Visibility");
tableColumn2.getColumn().setText("Description");
tableColumn7.getColumn().setText("Operation");
tableColumn8.getColumn().setText("Operation");
tableColumn1.getColumn().setWidth(80);
tableColumn2.getColumn().setWidth(140);
tableColumn7.getColumn().setWidth(80);
tableColumn8.getColumn().setWidth(80);
table.setHeaderVisible(true);
table.setLinesVisible(true);
FormData tableData = new FormData();
tableData.left = new FormAttachment(0, 0);
tableData.right = new FormAttachment(100, 0);
tableData.top = new FormAttachment(label, insetY);
tableData.bottom = new FormAttachment(button, -insetY);
table.setLayoutData(tableData);
Point size = label.computeSize(SWT.DEFAULT, SWT.DEFAULT);
final FormData labelData = new FormData(size.x, SWT.DEFAULT);
labelData.left = new FormAttachment(0, 0);
labelData.right = new FormAttachment(100, 0);
label.setLayoutData(labelData);
item.setControl(composite);
}
@Override
public void dateReceived(final Alarm alarm) {
Display.getDefault().asyncExec(new Runnable() {
@Override
public void run() {
final TableItem item = new TableItem(table, SWT.NONE);
final TableEditor operationEditors = new TableEditor(table);
final TableEditor checkEditors = new TableEditor(table);
final TableEditor removeEditors = new TableEditor(table);
operationButtons = new Button(table, SWT.PUSH);
removeButtons = new Button(table, SWT.PUSH);
checkButtons = new Button(table, SWT.CHECK);
checkButtons.computeSize(SWT.DEFAULT, table.getItemHeight());
checkButtons.setSelection(true);
checkEditors.grabHorizontal = true;
checkEditors.minimumHeight = checkButtons.getSize().y;
checkEditors.minimumWidth = checkButtons.getSize().x;
checkEditors.setEditor(checkButtons, item, 0);
removeButtons.computeSize(SWT.DEFAULT, table.getItemHeight());
removeButtons.setSelection(true);
removeButtons.setText("Remove");
removeEditors.grabHorizontal = true;
removeEditors.minimumHeight = removeButtons.getSize().y;
removeEditors.minimumWidth = removeButtons.getSize().x;
removeEditors.setEditor(removeButtons, item, 3);
operationButtons.setText("GO");
operationButtons.computeSize(SWT.DEFAULT, table.getItemHeight());
operationEditors.grabHorizontal = true;
operationEditors.minimumHeight = operationButtons.getSize().y;
operationEditors.minimumWidth = operationButtons.getSize().x;
operationEditors.setEditor(operationButtons, item, 2);
removeButtons.addListener(SWT.Selection, new Listener() {
@Override
public void handleEvent(Event e) {
if (e.widget instanceof Button) {
MessageBox messageBox = new MessageBox(composite
.getShell(), SWT.ICON_QUESTION | SWT.YES
| SWT.NO);
messageBox
.setMessage("Do you really want to remove?");
int response = messageBox.open();
if (response == SWT.YES) {
composite.getDisplay().asyncExec(
new Runnable() {
public void run() {
removeEditors.getEditor()
.dispose();
operationEditors.getEditor()
.dispose();
checkEditors.getEditor()
.dispose();
table.remove(table
.indexOf(item));
composite.layout(true, true);
composite.setSize(
composite.getSize().x,
composite.getSize().y - 1);
}
});
}
}
}
});// remove buttons operation that works fine for induvidial ,
// each row data and buttons.
item.setText(0, "");
item.setText(1, alarm.getDescription());
}
});
}}
修改:解决方案,但不是所希望的: org.eclipse.swt.widgets.Composite.getChildren();
table.getChildren()
返回一个子数组。现在,我通过迭代这个childen数组来处理按钮。它适用于删除表removeAll()
方法中残留的所有按钮。button.addListener(SWT.Selection, new Listener() {
@Override
public void handleEvent(Event e) {
if (e.widget instanceof Button) {
for (Control button : table.getChildren()) {
button.dispose();
}
table.removeAll();
}
}
});
答案 0 :(得分:0)
您不必为收到的每个警报/数据创建新的UI组件,让JFace为您完成,转到MVC模型。
然后,要实现全部删除,您只需要清除模型alarms.clear()
,然后让视图刷新tableViewer.refresh()
。
public Test(TabFolder tabFolder) {
this.tabFolder = tabFolder;
this.name = "Test";
TabItem item = new TabItem(this.tabFolder, SWT.NONE);
item.setText(this.name);
item.setToolTipText(this.name);
final int insetX = 4, insetY = 4;
FormLayout formLayout = new FormLayout();
formLayout.marginWidth = insetX;
formLayout.marginHeight = insetY;
composite = new Composite(tabFolder, SWT.NONE);
newSize = composite.computeSize(SWT.DEFAULT, SWT.DEFAULT, true);
composite.setLayout(formLayout);
Label label = new Label(composite, SWT.WRAP);
label.setText("List");
button = new Button(composite, SWT.NONE);
button.setText("Remove All");
button.setSize(new Point(100, 40));
FormData buttonData = new FormData();
buttonData.right = new FormAttachment(100, -insetX);
buttonData.bottom = new FormAttachment(100, 0);
button.setLayoutData(buttonData);
button.setVisible(true);
button.addListener(SWT.Selection, new Listener() {
@Override
public void handleEvent(Event e) {
alarms.clear();
tableViewer.refresh();
}
});
tableViewer = new TableViewer(composite, SWT.BORDER | SWT.FULL_SELECTION);
Table table = tableViewer.getTable();
TableViewerColumn tableColumn1 = new TableViewerColumn(tableViewer, SWT.CENTER);
TableViewerColumn tableColumn2 = new TableViewerColumn(tableViewer, SWT.CENTER);
TableViewerColumn tableColumn7 = new TableViewerColumn(tableViewer, SWT.CENTER);
TableViewerColumn tableColumn8 = new TableViewerColumn(tableViewer, SWT.CENTER);
tableColumn1.getColumn().setText("Visibility");
tableColumn2.getColumn().setText("Description");
tableColumn7.getColumn().setText("Operation");
tableColumn8.getColumn().setText("Operation");
tableColumn1.getColumn().setWidth(80);
tableColumn2.getColumn().setWidth(140);
tableColumn7.getColumn().setWidth(80);
tableColumn8.getColumn().setWidth(80);
table.setHeaderVisible(true);
table.setLinesVisible(true);
FormData tableData = new FormData();
tableData.left = new FormAttachment(0, 0);
tableData.right = new FormAttachment(100, 0);
tableData.top = new FormAttachment(label, insetY);
tableData.bottom = new FormAttachment(button, -insetY);
table.setLayoutData(tableData);
Point size = label.computeSize(SWT.DEFAULT, SWT.DEFAULT);
final FormData labelData = new FormData(size.x, SWT.DEFAULT);
labelData.left = new FormAttachment(0, 0);
labelData.right = new FormAttachment(100, 0);
label.setLayoutData(labelData);
// defaine model
tableViewer.setContentProvider(ArrayContentProvider.getInstance());
tableViewer.setInput(alarms);
// labels providers
tableViewer.setLabelProvider(new CellLabelProvider() {
@Override
public void update(ViewerCell cell) {
switch (cell.getColumnIndex()) {
case 1:
cell.setText(" Description "+ cell.getElement().toString());
break;
case 2:
cell.setText("GO");
break;
case 3:
cell.setText("Remove");
break;
default:
break;
}
}
});
// editing support
tableColumn7.setEditingSupport(new CellEditingSupport(tableViewer){
@Override
protected void clicked(Object element) {
System.err.println("GO clicked");
}
});
tableColumn8.setEditingSupport(new CellEditingSupport(tableViewer){
@Override
protected void clicked(Object element) {
MessageBox messageBox = new MessageBox(composite.getShell(), SWT.ICON_QUESTION | SWT.YES | SWT.NO);
messageBox.setMessage("Do you really want to remove?");
if (messageBox.open() == SWT.YES) {
alarms.remove(element);
tableViewer.refresh();
}
}
});
}
abstract class CellEditingSupport extends EditingSupport {
private final TableViewer viewer;
private CheckboxCellEditor cellEditor;
public CellEditingSupport(TableViewer viewer) {
super(viewer);
this.viewer = viewer;
}
@Override
protected CellEditor getCellEditor(Object element) {
if (cellEditor == null) {
cellEditor = new CheckboxCellEditor(viewer.getTable());
}
return cellEditor;
}
@Override
protected boolean canEdit(Object element) {
return true;
}
@Override
protected Object getValue(Object element) {
return true;
}
@Override
protected void setValue(Object element, Object value) {
clicked(element );
}
protected abstract void clicked(Object element) ;
}