Search code examples
javaswtjfacetableviewer

Select a row in TableViewer


I have a TableViewer in my application that has the following features:

  • It gets data from a web service, and upon selecting a row, buttons get enabled and you can make operations (all these operations are calling the webservice, so the table stays in sync with the database).
  • When I add a new line, I submit an "add" command to the web service, and refresh the table. Now I have a new line, and I know which line is the new one.

Now I want to select the new line by default, and I tried many commands like tableViewer.getTable().select(index); and as it is now:

public void selectAdded(int id) {
    tableViewer.getTable().setSelection(id);
}

This picture shows what is the problem: The two different type of selections

The upper one shows as it works now with this code. The row below shows how it looks when I click a row. The problem is, the buttons don't get enabled, and I have two read-only text fields that remains blank instead of showing the information I need. But when I click, everything works normally.

What should I do to achive that selectAdded(int id) acts like a click? The solution should be multi-platform (Mac/Windows), but at least on Mac.


Solution

  • Don't use the Table selection methods which using TableViewer. Instead use

    ISelection selection = new StructuredSelection(model object);
    
    tableViewer.setSelection(selection);
    

    'model object' is the model object for the row you want to select (as returned by your content provider).