我创建了一个包含3列的swt表,第一列是check列。我用了这段代码:
table = new Table(container, SWT.CHECK | SWT.BORDER | SWT.V_SCROLL
| SWT.H_SCROLL|SWT.MULTI);
当我选择一个项目时,会在第三个冒号中创建一个文本。代码如下:
listener = new Listener() {
@Override public void handleEvent(Event event) {
if (event.detail == SWT.CHECK) {
final TableItem current = (TableItem) event.item;
if (current.getChecked()) {
final TableEditor editor = new TableEditor(table);
text = new Text(table, SWT.NONE);
editor.grabHorizontal = true;
}
我想获取与第三列匹配所选项目的单元格的值,但无法使用selectedItem.getText(2)
获取该值。
有什么帮助吗?
答案 0 :(得分:1)
试试这个代码示例。它将打印出所选TableItem
的第3列中的文字:
public static void main(String[] args)
{
Display display = Display.getDefault();
final Shell shell = new Shell(display);
shell.setLayout(new GridLayout(1, true));
Table table = new Table(shell, SWT.CHECK | SWT.MULTI);
table.setHeaderVisible(true);
table.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
for(int i = 0; i < 4; i++)
{
TableColumn column = new TableColumn(table, SWT.NONE);
column.setText("Column " + i + " ");
column.pack();
}
for(int i = 0; i < 10; i++)
{
TableItem newItem = new TableItem(table, SWT.NONE);
newItem.setText(1, "ITEM " + i + " TEXT1");
newItem.setText(2, "ITEM " + i + " TEXT2");
newItem.setText(3, "ITEM " + i + " TEXT3");
}
table.addListener(SWT.Selection, new Listener()
{
@Override
public void handleEvent(Event event)
{
if(event.detail == SWT.CHECK)
{
TableItem current = (TableItem)event.item;
if(current.getChecked())
{
System.out.println(current.getText(2));
}
}
}
});
shell.pack();
shell.open();
while (!shell.isDisposed())
{
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
这就是它的样子: