I have Table which has SWT.CHECK style so I can remove checked items. I add button and SelectionAdapter to do the job:
Button btnRemove = new Button(this, SWT.PUSH);
btnRemove.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
TableItem[] items = table.getItems(); /* get all items */
int i;
for (i = 0;i < items.length;i++)
if (items[i].getChecked()) /* look if it is selected */
table.remove(i);
}
});
When I remove single item, it is working fine, but when I check multiple items and then hit remove, I get SWTException: Widget is disposed. Here is the full stack trace:
org.eclipse.swt.SWTException: Widget is disposed
at org.eclipse.swt.SWT.error(SWT.java:4282)
at org.eclipse.swt.SWT.error(SWT.java:4197)
at org.eclipse.swt.SWT.error(SWT.java:4168)
at org.eclipse.swt.widgets.Widget.error(Widget.java:468)
at org.eclipse.swt.widgets.Widget.checkWidget(Widget.java:340)
at org.eclipse.swt.widgets.TableItem.getChecked(TableItem.java:423)
at org.majid.swt.Main$1.widgetSelected(Main.java:132)
at org.eclipse.swt.widgets.TypedListener.handleEvent(TypedListener.java:240)
at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:84)
at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1053)
at org.eclipse.swt.widgets.Display.runDeferredEvents(Display.java:4165)
at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:3754)
at org.majid.swt.Main.main(Main.java:24)
- Could anyone help with this problem?
- Is there any method like
TableItems[] getCheckedItems()to retrieve only check items instead of checking whole items?
Once you call
table.remove(i);the items in the table will be different (one less) than in your array and in the next iteration the i-th item in the table is not what you expect.