tags:

views:

812

answers:

2

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
...

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
}
...

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

A: 

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
     }
OTisler
in this example I expect getText(0) to return "TEST" and getText(1) to return the combo-box selection ("A", "B" or "C"). But it seems it doesn't work this way. Is there a way to achieve this
amarnath vishwakarma
A: 

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

amarnath vishwakarma