我尝试从 SWT 表中删除所有当前行,然后用新数据替换行。
我创建了示例代码以显示我遇到问题的位置。在该示例中,双击侦听器将调用其中一个方法,以尝试从表中删除当前数据。通过repopulate()
方法替换它。
SWT表申请来源:
package javaTools;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
public class QuickTableExampleA {
public static void main(String[] args) {
String data = "ljames@ubunzeus: ~|0x0480002c\n"
+ "Stack Overflow - Where Developers Learn, Share, & Build"
+ " Careers - Google Chrome|0x0520000a\n"
+ "Inbox - L. D. James - Mozilla Thunderbird|0x05208736\n";
Display display = new Display();
Shell shell = new Shell(display);
shell.setSize(700, 500);
shell.setText("My Table Smipplet");
shell.setLayout(new FillLayout());
Table table = new Table(shell, SWT.BORDER);
TableColumn column1 = new TableColumn(table, SWT.LEFT);
TableColumn column2 = new TableColumn(table, SWT.LEFT);
column1.setText("Window");
column2.setText("Window ID");
column1.setWidth(600);
column2.setWidth(70);
table.setHeaderVisible(true);
String[] array = data.split("\n");
for (int i = 0; i < array.length; i++) {
// System.out.println(array[i]);
String[] array2 = array[i].split("\\|");
TableItem newrow = new TableItem(table, SWT.NONE);
newrow.setText(new String[] { array2[0], array2[1] });
}
table.addListener(SWT.MouseDoubleClick, new Listener()
{
@Override
public void handleEvent(Event event)
{
Point pt = new Point(event.x, event.y);
TableItem item = table.getItem(pt);
if (item != null)
{
/* Iterate over all columns and check if event is contained */
for (int col = 0; col < table.getColumnCount(); col++)
{
Rectangle rect = item.getBounds(col);
if (rect.contains(pt))
{
System.out.println("The WindowID is" + item.getText(1));
/*
The following methods are my efforts to remove the
current data so that only the new repopulated data
will be in the table.
*/
// table.removeAll();
// table.clearAll();
// removerowbyrow();
repopulateTable();
}
}
}
}
private void removerowbyrow() {
int rowcount = table.getItemCount();
System.out.println("Number of rows: " + rowcount);
System.out.println("Removing all rows 1 by 1");
for(int i = 0; i <table.getItemCount(); i++) {
rowcount = table.getItemCount();
System.out.println("Removing the first row of: " + rowcount);
table.remove(0);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
private void repopulateTable() {
System.out.println("This is a repopulateTable.");
String[] array = getdata();
for (int i = 0; i < array.length; i++) {
// System.out.println(array[i]);
String[] array2 = array[i].split("\\|");
TableItem newrow = new TableItem(table, SWT.NONE);
newrow.setText(new String[] { array2[0], array2[1] });
}
}
});
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
public static String[] getdata() {
String data = "swt table delete records - Google Search - Google Chrome|0x0480002c\n"
+ "javaTools/src/javaTools/QuickTableExampleA.java -"
+ " /home/users/l/j/ljames/workspace - Eclipse|0x05a000aa\n"
+ "swt java how to delete a range of rows - Google Search - Google Chrome|0x05208736\n";
String[] array = data.split("\n");
return array;
}
}
我有三个被注释掉的功能。那是因为程序会在每个方法上崩溃。我希望有人能够确定这三个功能有什么问题。他们是:
取消注释时,三个函数的错误输出为:
table.removeAll();
The WindowID is0x0520000a
This is a repopulateTable.
Exception in thread "main" org.eclipse.swt.SWTException: Widget is disposed
at org.eclipse.swt.SWT.error(Unknown Source)
at org.eclipse.swt.SWT.error(Unknown Source)
at org.eclipse.swt.SWT.error(Unknown Source)
at org.eclipse.swt.widgets.Widget.error(Unknown Source)
at org.eclipse.swt.widgets.Widget.checkWidget(Unknown Source)
at org.eclipse.swt.widgets.TableItem.getBounds(Unknown Source)
at javaTools.QuickTableExampleA$1.handleEvent(QuickTableExampleA.java:62)
at org.eclipse.swt.widgets.EventTable.sendEvent(Unknown Source)
at org.eclipse.swt.widgets.Display.sendEvent(Unknown Source)
at org.eclipse.swt.widgets.Widget.sendEvent(Unknown Source)
at org.eclipse.swt.widgets.Display.runDeferredEvents(Unknown Source)
at org.eclipse.swt.widgets.Display.readAndDispatch(Unknown Source)
at javaTools.QuickTableExampleA.main(QuickTableExampleA.java:111)
table.clearAll();
这个没有给出错误。它留下空白行。重新填充显示在前一个日期存在的下方。它不会替换表格,而是附加在表格上。
repopulateTable();
The WindowID is0x0520000a
Number of rows: 3
Removing all rows 1 by 1
Removing the first row of: 3
Removing the first row of: 2
This is a repopulateTable.
Exception in thread "main" org.eclipse.swt.SWTException: Widget is disposed
at org.eclipse.swt.SWT.error(Unknown Source)
at org.eclipse.swt.SWT.error(Unknown Source)
at org.eclipse.swt.SWT.error(Unknown Source)
at org.eclipse.swt.widgets.Widget.error(Unknown Source)
at org.eclipse.swt.widgets.Widget.checkWidget(Unknown Source)
at org.eclipse.swt.widgets.TableItem.getBounds(Unknown Source)
at javaTools.QuickTableExampleA$1.handleEvent(QuickTableExampleA.java:62)
at org.eclipse.swt.widgets.EventTable.sendEvent(Unknown Source)
at org.eclipse.swt.widgets.Display.sendEvent(Unknown Source)
at org.eclipse.swt.widgets.Widget.sendEvent(Unknown Source)
at org.eclipse.swt.widgets.Display.runDeferredEvents(Unknown Source)
at org.eclipse.swt.widgets.Display.readAndDispatch(Unknown Source)
at javaTools.QuickTableExampleA.main(QuickTableExampleA.java:115)
答案 0 :(得分:1)
“Widget is dispos”错误是因为您在已被处置(丢弃)的表项上调用TableItem.getBounds
。
致电Table.removeAll
或Table.remove
后,您必须终止正在查看旧表项目 的循环。此时您无法对旧项目做任何进一步的操作。
Table.clearAll
不会丢弃它只是将它们设置为空白的旧表项,所以这就是它应该做的事情。
因此,总而言之,使用Table.removeAll
,但是当你这样做时立即停止循环 - 看起来你只需要一个break
:
for (int col = 0; col < table.getColumnCount(); col++)
{
Rectangle rect = item.getBounds(col);
if (rect.contains(pt))
{
System.out.println("The WindowID is" + item.getText(1));
table.removeAll();
repopulateTable();
// Stop loop now, further reference to 'item' is invalid
break;
}
}