很难分辨出你在挣扎的是哪一部分,但我假设你没有从你想要的表中得到价值。
你在打电话吗
TableItem#getText()
,它将为您提供该行第一列的值。
如果你想在一个特定的列获取文本,你必须调用
TableItem#getText(int)
下面的例子应该说明这一点。它显示一个包含三列的表,当您单击一个单元格时,它将打印该单元格的值。
public static void main(String[] args)
{
final Display display = new Display();
final Shell shell = new Shell(display);
shell.setLayout(new FillLayout());
Table table = new Table(shell, SWT.V_SCROLL | SWT.H_SCROLL | SWT.BORDER | SWT.FULL_SELECTION);
int cols = 3;
for (int c = 0; c < cols; c++)
{
TableColumn column = new TableColumn(table, SWT.NONE);
column.setText("Column " + c);
}
int rows = 10;
for (int r = 0; r < rows; r++)
{
TableItem item = new TableItem(table, SWT.NONE);
for (int c = 0; c < cols; c++)
{
item.setText(c, r + " " + c);
}
}
for (int c = 0; c < cols; c++)
table.getColumn(c).pack();
table.addListener(SWT.MouseDown, e -> {
Point pt = new Point(e.x, e.y);
TableItem item = table.getItem(pt);
if (item != null)
{
for (int c = 0; c < table.getColumnCount(); c++)
{
Rectangle rect = item.getBounds(c);
if (rect.contains(pt))
{
System.out.println(item.getText(c));
}
}
}
});
shell.pack();
shell.open();
shell.setSize(400, 300);
while (!shell.isDisposed())
{
if (!display.readAndDispatch())
{
display.sleep();
}
}
display.dispose();
}