2

我在桌子上放置了不同的控件,使用TableEditor.

...
TableItem [] items = table.getItems ();
for (int i=0; i<items.length; i++) {
    TableEditor editor = new TableEditor (table);
    final Text text1 = new Text (table, SWT.NONE);
    text1.setText(listSimOnlyComponents.get(i).getName());
    text1.setEditable(false);
    editor.grabHorizontal = true;
    editor.setEditor(text1, items[i], 0);

    editor = new TableEditor (table);
    final CCombo combo1 = new CCombo (table, SWT.NONE);
    combo1.setText("");
    Set<String> comps = mapComponentToPort.keySet();
    for(String comp:comps)
        combo1.add(comp);
    editor.grabHorizontal = true;
    editor.setEditor(combo1, items[i], 1);
} //end of for
...

当我尝试使用 获取表格上的文本时getItem(i).getText,我得到空字符串

...
TableItem [] items = table.getItems ();
for(int i=0; i<items.length; i++) {
    TableItem item = items[i];
    String col0text = items[i].getText(0);  //this text is empty
    String col1text = items[i].getText(1);  //this text is empty
}
...

为什么即使我有文本出现在表格上,getText 也会返回空字符串?

4

3 回答 3

3

您可以考虑使用 data 属性,而不是使用 text 属性。

好处:

  1. 您可以将数据(例如控件或复杂数据结构)直接附加到表项。
  2. 您的表格项目创建需要从表格单元格编辑器中知道的唯一事情是要存储在数据属性中的对象引用。
  3. 你不需要那些事件处理的东西(当你真正需要的时候读取控件数据)。

创造:

TableItem item = new TableItem(table, SWT.None);
TableEditor editor = new TableEditor(table);
Button checkbox = new Button(table, SWT.CHECK);
checkbox.pack();
editor.setEditor(checkbox,item,0);
item.setData("cb",checkbox);   // using key enables you to add more pieces of complex data

读:

for (TableItem item : table) {
  Button checkbox = (Button) item.getData("cb");
  if (checkbox.getSelection()) { /* ... do whatever you want */ }
}

当表格出现时,复选框可见并且可以单击。在透明背景控件的情况下使用 setText 方法会失败 -> 您将在控件下方看到表格项单元格的文本(我试过了)。

无论如何,如果可以扩展表项类以隐藏数据键,那会容易得多。但像往常一样,子类化被拒绝。

于 2014-09-20T20:49:40.090 回答
1

在我添加的控件的事件侦听器中

  item.setText call
  ...
  combo1.addSelectionListener(new SelectionAdapter() {
  public void widgetSelected(SelectionEvent evt) {
              String sel = combo2.getText();
      item.setText(ComponentPortToConnectCol, sel);
}});
  ...

这给了我想要的结果。感谢 OTisler 提供线索

于 2009-03-11T16:31:55.490 回答
0

你是如何布置餐桌的?我复制了您的代码以对其进行调试并如下所示设置表(第一个 for 循环)
我尝试但无法重现您看到的问题。这可能是您向表格添加内容的方式。
表格编辑器示例

Display display = new Display();
        Shell shell = new Shell(display);
        shell.setLayout(new FillLayout());
        final Table table = new Table(shell, SWT.BORDER | SWT.MULTI);
        for (int i = 0; i < 3; i++) {
            TableItem item = new TableItem(table, SWT.NONE);
            item.setText(new String[] { "" + i, "" + i, "" + i });
        }
        TableItem [] items = table.getItems();
        for (int i=0; i<items.length; i++) {
            TableEditor editor = new TableEditor (table);
            final Text text1 = new Text (table, SWT.NONE);
            text1.setText("TEST");
            text1.setEditable(false);
            editor.grabHorizontal = true;
            editor.setEditor(text1, items[i], 0);

            editor = new TableEditor (table);
            final CCombo combo1 = new CCombo (table, SWT.NONE);
            combo1.setText("");
            String[] comps = {"A","B","C"};
            for(String comp:comps)
                combo1.add(comp);
            editor.grabHorizontal = true;
            editor.setEditor(combo1, items[i], 1);
        } //end of for


        TableItem [] items2 = table.getItems ();
        for(int i=0; i<items2.length; i++) {
            TableItem item = items2[i];
            String col0text = items2[i].getText(0);  //returns '0' first for loop
        }
于 2009-03-11T15:49:03.033 回答