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

走远了吗. 提交于 2019-12-06 04:09:00

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

Benefits:

  1. you can attach the data (e.g. a control or complex data structure) directly to the table item.
  2. the only thing your table item creation need to know from your table cell editor is the object reference to be stored in the data property.
  3. you don't need that event handling stuff (just read the controls data when you really need it).

Create:

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

Read:

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

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.

amarnath vishwakarma

in the event listeners for the controls I added

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

This gives me the desired result. Thanks OTisler for the clue

How are you setting up your table? I copied your code to debug it and set up the table as shown below (first for loop)
I tried but couldn't reproduce the problem you're seeing. It might be the way you are adding things to the table.
TableEditor Examples

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
        }
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!