我正在尝试使用syncexec自动更新此表。我粘贴的代码是一个测试代码,它会产生我遇到的相同症状,但是我使用随机数生成实用程序而不是来自外部源的实时数据。
我有2列。一列是一个1-9号的地方。第二列代表实时数据,并且经常会发生变化。我想要更新第二列。
我目前只是添加指示实时数据的行,但我希望总共只有9行,而不是几毫秒内添加的9行。我知道这样做是因为我在syncexec中一遍又一遍地填充表格。我如何使用syncexec更新我在UI中建立的表?
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import java.util.Random;
public class TableAutoUpdate
{
int u = 10;
int i;
int r;
int d = 10;
int c = 1;
int v = u+1;
boolean a = false;
private Label label; //Must import org.eclipse.swt.widgets.Label; then declare it in the class to use it.
private Label status;
private Display display;
private Shell shell = new Shell(display, SWT.SHELL_TRIM | SWT.CENTER );
public TableAutoUpdate(Display display)
{
try
{
initUI(display);
invalidateUiData();
}
catch (NoClassDefFoundError e)
{
status.setText(e.toString());
}
}
private void initUI(Display display)
{
shell.setLayout(new GridLayout(3, false));
String RegisterLabels[] = new String[u];
status = new Label(shell, SWT.BORDER);
GridData gd2 = new GridData(SWT.FILL, SWT.FILL, true, true);
status.setLayoutData(gd2);
gd2.widthHint = 40;
gd2.heightHint = 15;
gd2.horizontalSpan = 3;
Table table = new Table(shell, SWT.BORDER);
table.setHeaderVisible(true);
table.setLinesVisible(true);
TableColumn tc1 = new TableColumn(table, SWT.CENTER);
TableColumn tc2 = new TableColumn(table, SWT.CENTER);
tc1.setText("Register Number");
tc2.setText("Register Value");
tc1.setWidth(110);
tc2.setWidth(110);
int b = 1;
Random randomGen = new Random();
int randomInt = randomGen.nextInt(32000);
for(i=1; i<u; i++)
{
status.setText("Valid numbers");
TableItem item1 = new TableItem(table, SWT.NONE);
RegisterLabels[i] = Integer.toString(b);
item1.setText(new String[] {RegisterLabels[i], item1.getText(randomInt)});
b++;
}
shell.setSize(300, 300);
status.getText();
shell.setText("Modbus Tags");
centerWindow(shell);
shell.open();
new Thread()
{
@Override
public void run()
{
for (final int l = 1; l <= l; )
{
try
{
Thread.sleep (100);
}
catch (Throwable th)
{
}
if (display.isDisposed()) return;
display.syncExec(new Runnable()
{
@Override
public void run()
{
try
{
int b = 1;
for (r = 1; r < u; r++)
{
Random randomGen = new Random();
int randomInt = randomGen.nextInt(32000);
status.setText("Random Number Table Update");
TableItem item1 = new TableItem(table, SWT.NONE);
RegisterLabels[r] = Integer.toString(b);
item1.setText(new String[] {RegisterLabels[r], Integer.toString(randomInt)});
b++;
}
}
catch (Exception e)
{
status.setText("Invalid Number");
try
{
Thread.sleep (1000);
}
catch (Throwable th)
{
}
invalidateUiData();
}
}
});
}
}
}.start();
while (!shell.isDisposed())
{
if (!display.readAndDispatch())
display.sleep();
}
}
void invalidateUiData()
{
status.setText("Unable to numbers.");
}
private void centerWindow(Shell shell) {
Rectangle bds = shell.getDisplay().getBounds();
Point p = shell.getSize();
int nLeft = (bds.width - p.x) / 2;
int nTop = (bds.height - p.y) / 2;
shell.setBounds(nLeft, nTop, p.x, p.y);
}
@SuppressWarnings("unused")
public static void main(String[] args) {
Display display = new Display();
TableAutoUpdate ex = new TableAutoUpdate(display);
display.dispose();
}
}
答案 0 :(得分:0)
为开头的行创建9个TableItem。
然后使用:
TableItem item = table.get(rowNumber);
item.setText(text);
获取现有项目并设置其文本。请注意,rowNumber从0开始。