views:

235

answers:

2

I use the TableViewer to show informations in a table. The user can select one of the shown options by selecting one line of the table.

I want to create a table in matrix form, in which the user can not only select the line. It should be possible to select every item of the table, like row 2 column 3. For every item selection an action is called to handle this item as it is in the TableViewer.

As far as i now, i can add CellModifier and CellEditors to the line of Columns of the table, but the reference for the action is always the line object and not the selected TableItem.

Does somebody have an example how to create such a matrix inside a Composite? I can create it by setting a GridLayout and adding the components in a for-loop, but than i get issues, when i want to redraw the Composite with new childrens. The TableViewer does already have this handling, so i dont want to implement it again.

+1  A: 

I had the same problem a while ago and the only way I found to solve it was to register a mouse listener on the SWT table widget associated to the table viewer.

MouseListener columnSelectionMouseListener = new ColumnSelectionMouseListener();
getViewer().getTable().addMouseListener(columnSelectionMouseListener);

public class ColumnSelectionMouseListener implements MouseListener {

    private TableColumn selectedColumn;

    @Override
    public void mouseDoubleClick(MouseEvent e) {
     // Nothing to do here
    }

    @Override
    public void mouseDown(MouseEvent e) {
     table = (Table) e.widget;
 TableItem item = table.getItem(new Point(e.x, e.y));
 for (int i = 0; i < table.getColumnCount(); i++) {
  TableColumn column = table.getColumn(i);
  Rectangle bounds = item.getBounds(i);
  if (bounds.contains(e.x, e.y)) {
   selectedColumn = column;
  }
 }
    }

    @Override
    public void mouseUp(MouseEvent e) {
     // Nothing to do here
    }

    public TableColumn getSelectedField() {
           return selectedColumn;
    }
}

Then, for example in the viewer's selection listener, you can ask to the mouse listener which column was selected when the mouse has been pressed and combine that with the selected line coming from the viewer's selection to perform the appropriate action.

Hope this can help.

Manu

Manuel Selva
A: 

Maybe the following JFace snippet will help: Snippet058CellNavigationIn34

Ingo