gpt4 book ai didi

java - 如何从 Java/SWT 表中删除新数据将替换旧数据的所有行?

转载 作者:行者123 更新时间:2023-12-02 12:38:51 33 4
gpt4 key购买 nike

我正在尝试从 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();
  • table.clearAll();
  • 删除rowbyrow();

取消注释后,三个函数的错误输出为:

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)

最佳答案

“小部件已处置”错误是因为您在已处置(丢弃)的表格项上调用 TableItem.getBounds

一旦调用Table.removeAllTable.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;
}
}

关于java - 如何从 Java/SWT 表中删除新数据将替换旧数据的所有行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45037205/

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