John Smith
John Smith

Reputation: 787

How to get tableviewer row items?

I have a tableviewer inside a plugin with a view. The tableviewer has 5 columns. I need the data in the rows when I select the row. For example: when I select the row 1, the listener should store in a variable the data for column 1 row 1.

I have used this to get the data in the selected row but it returns the data for the first column only:

table.addListener(SWT.DefaultSelection, new Listener() {       
    public void handleEvent(Event event) {
          TableItem[] selection = table.getSelection();
          for (int i = 0; i < selection.length; i++)
          {                 
          System.out.println(selection[i].getText());
          }                    
    }
  });

How to get the data in all the columns of the row?

Upvotes: 1

Views: 1549

Answers (1)

greg-449
greg-449

Reputation: 111217

Use TableViewer.addSelectionChangedListener

viewer.addSelectionChangedListener(new ISelectionChangedListener() {
  @Override
  public void selectionChanged(final SelectionChangedEvent event)
  {
    IStructuredSelection selection = (IStructuredSelection)event.getSelection();

    RowData rowData = (RowData)selection.getFirstElement();

    ....
  }
});

where RowData is your model object for the row

Upvotes: 1

Related Questions