如何使用任意控件获取swt表上的文本内容 [英] How to get the text content on the swt table with arbitrary controls

查看:137
本文介绍了如何使用任意控件获取swt表上的文本内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 TableEditor 在表格上放置了不同的控件。

I have different controls placed on a table using 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 获取表格中的文字,我得到空字符串

When I try to get the text on the table using getItem(i).getText, I get empty string

...
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即使表格中出现文字,也会返回空字符串?

Why does getText returns empty strings even when I have text appearing on the table?

推荐答案

您可以考虑使用数据属性。

Instead of using the text property, you might consider using the data property instead.

福利:


  1. 您可以附加数据(例如一个控件或复杂的数据结构)直接到表项。

  2. 您的表项创建需要从表格单元格编辑器中知道的唯一内容是要存储在data属性中的对象引用。

  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方法失败 - >你会看到
下面的表项单元格的文本(我试过这个)。

When the table shows up, the checkbox is visible and can be clicked. Using the setText method fails in case of transparent background controls -> you will see the text of the table items cell below your control (I tried this).

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

Anyway it would be much easier if it would be possible to extend the table item class to hide even the data key. But as usual, subclassing is denied.

这篇关于如何使用任意控件获取swt表上的文本内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆