簡體   English   中英

如何使用任意控件獲取 swt 表上的文本內容

[英]How to get the text content on the swt table with arbitrary controls

我使用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 也會返回空字符串?

您可以考慮使用 data 屬性,而不是使用 text 屬性。

好處:

  1. 您可以將數據(例如控件或復雜數據結構)直接附加到表項。
  2. 您的表格項目創建需要從表格單元格編輯器中知道的唯一事情是要存儲在數據屬性中的 object 引用。
  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 方法會失敗 -> 您將在控件下方看到表格項單元格的文本(我試過了)。

無論如何,如果可以擴展表項 class 以隱藏數據鍵,那會容易得多。 但像往常一樣,子類化被拒絕。

在我添加的控件的事件偵聽器中

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

這給了我想要的結果。 感謝 OTisler 提供線索

你是如何布置餐桌的? 我復制了您的代碼以對其進行調試並設置表格,如下所示(第一個 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
        }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM