views:

674

answers:

3

What I'd like to do is be able to tab between elements in table.

I currently am creating my table like this.

this.tableViewer = 
            new TableViewer(parent , SWT.FULL_SELECTION);

   tableViewer.setUseHashlookup(true);
        table = tableViewer.getTable();

        GridData gridData = new GridData(GridData.FILL_BOTH);
        gridData.grabExcessVerticalSpace = true;
        table.setLayoutData(gridData);

        table.setLinesVisible(true);
        table.setHeaderVisible(true);

        ...

   /** Create the Cell Editor Array - will hold all columns **/
        editors = new CellEditor[table.getColumnCount()];
        /** Cell Editor Row 1 **/


  /** Set the column properties **/
        tableViewer.setColumnProperties(columnNames);

        /** Assign the cell editors to the viewer **/
        tableViewer.setCellEditors(editors);

        /** Set the cell modifier for the viewer **/
        tableViewer.setCellModifier(new MyCellModifier(this));
        //Create the Table Viewer

        /** Table Viewer Content and Label Provider **/
        tableViewer.setContentProvider(new MyContentProvider(this));
        tableViewer.setLabelProvider(new MyLabelProvider());

But I'm not sure how to set up the tabulation. Everything else works as far as editing columns, showing data, etc. Just stuck on this last part.

If I've missed obvious documentation or javadocs - my apologies and even pointing to those would be great.

+1  A: 

You need to add a KeyListener and set the selection or focus to the next cell:

tableViewer.getTable().addKeyListener(new KeyListener(){

   public void keyPressed(KeyEvent e) {

       System.out.println("Key Pressed");
       if (e.keycode == SWT.TAB)
       {
           System.out.println("Detected TAB key");
           // set table viewer selection
       }
   }

   public void keyReleased(KeyEvent e) {

       System.out.println("Key Released");
   }}
);
Droo
org.eclipse.jface.viewers.TableViewer does not accept a KeyListener
PSU_Kardi
Don't know if it works, but tableViewer.getTable() should accept it.
derBiggi
That was the problem
PSU_Kardi
+2  A: 

I think by default tab does not jump from cell to cell in an swt table. Instead it traverses to the next control. So you'll also need to tell it not to traverse when tab is pressed

KeyListener keyListener = new KeyLisener()
{
    public void keyPressed(KeyEvent evt)
    {
        if (evt.keyCode == SWT.TAB)
        {
            // There are numerous setSelection methods.  I'll leave this to you. 
            tableViewer.getTable().setSelection(...)
        }
    }

    public void keyReleased(KeyEvent evt){}
}

TraverseListener traverseListener = new TraverseListener()
{
    public void keyTraversed(TraverseEvent evt)
    {
        if (evt.keyCode == SWT.TAB)
            evt.doit = false;
    }
}

tableViewer.getTable().addKeyListener(keyListener);
tableViewer.getTable().addTraverseListener(traverseListener);

Also, as derBiggi suggested, the listeners need to be added to the Table object, not the TableViewer.

thehiatus
+4  A: 

Although the solution thehiatus posted is very low level and will probably work (I haven't tested it), JFace gives you a framework for this specific problem. See the org.eclipse.jface.viewers.TableViewerFocusCellManager along with org.eclipse.jface.viewers.CellNavigationStrategy classes to solve this problem.

arcticpenguin