Tab between fields in TableViewer
Asked Answered
D

5

6

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.

Decontrol answered 30/9, 2009 at 22:8 Comment(1)
In what direction do you want to tab?Phobe
T
7

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.

Tal answered 7/10, 2009 at 17:7 Comment(1)
The code could be simplified if the selection code (tableViewer.getTable().setSelection(...) in this example) was put into the keyCode == SWT.TAB branch of the TraverseListener. This would spare an extra KeyListener.Chopper
T
9

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.

Templas answered 9/10, 2009 at 14:43 Comment(1)
That's a useful pointer, but the answer would be nicer with some sample code or explanation how to use these precisely.Soutane
T
7

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.

Tal answered 7/10, 2009 at 17:7 Comment(1)
The code could be simplified if the selection code (tableViewer.getTable().setSelection(...) in this example) was put into the keyCode == SWT.TAB branch of the TraverseListener. This would spare an extra KeyListener.Chopper
G
3

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);
Gastrin answered 29/2, 2016 at 4:3 Comment(0)
M
1

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");
   }}
);
Macmullin answered 1/10, 2009 at 1:52 Comment(2)
org.eclipse.jface.viewers.TableViewer does not accept a KeyListenerDecontrol
Don't know if it works, but tableViewer.getTable() should accept it.Taction
C
0

I also had to implement tabbing between elements in a table. We use Grid from Nebula as the table. Firstly, I had to suppress tabbing the focus preventing it from moving out of the table. enter image description here

and then I added a Key Listener which moves the focus/selection to the next cell:

enter image description here

I also made my own algorithm to move the selection one cell to the right and when at the end of the row, move it to the beginning of the next row. When end of table is reached, the selection moves back to the first cell in the table.

This solved the problem for me.

Cytokinesis answered 4/7, 2016 at 12:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.