Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tab between fields in TableViewer

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.

like image 745
ist_lion Avatar asked Sep 30 '09 22:09

ist_lion


4 Answers

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.

like image 155
andyczerwonka Avatar answered Sep 22 '22 11:09

andyczerwonka


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.

like image 27
thehiatus Avatar answered Sep 23 '22 11:09

thehiatus


I couldn't get the desired behavior with a TraverseListener (it would not traverse within the table), and I had trouble getting it to work with a FocusCellManager and CellNavigationStrategy. I finally found this solution that enables me to tab from column to column within a row and automatically activate the editor.

Viewer viewer =  ...

TableViewerFocusCellManager focusCellManager =
    new TableViewerFocusCellManager(
        viewer,
        new FocusCellHighlighter(viewer) {});

ColumnViewerEditorActivationStrategy editorActivationStrategy =
    new ColumnViewerEditorActivationStrategy(viewer) {

            @Override
            protected boolean isEditorActivationEvent(
                ColumnViewerEditorActivationEvent event) {
                    ViewerCell cell = (ViewerCell) event.getSource();
                   return cell.getColumnIndex() == 1 || cell.getColumnIndex() == 2;
            }

};

TableViewerEditor.create(viewer, focusCellManager, editorActivationStrategy,
    TableViewerEditor.TABBING_HORIZONTAL);
like image 23
MidnightJava Avatar answered Sep 24 '22 11:09

MidnightJava


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");
   }}
);
like image 43
Droo Avatar answered Sep 23 '22 11:09

Droo