Tab between fields in TableViewer

I would like to make a tab between the elements in the table.

I am currently creating my table as follows.

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 a tab. Everything else works with editing columns, showing data, etc. Just stuck in this last part.

If I missed the obvious documentation or javadocs - my apologies and even pointing to them would be great.

+6
java eclipse swt jface
source share
5 answers

I think that by default the tab does not jump from cell to cell in the swt table. Instead, it moves on to the next control. This way, you will also need to say that it does not overlap when you click the tab

 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, listeners should be added to the Table object, not the TableViewer.

+7
source share

Although thehiatus solution is published very low and will probably work (I have not tested it), JFace gives you the foundation for this particular problem. See org.eclipse.jface.viewers.TableViewerFocusCellManager together with the org.eclipse.jface.viewers.CellNavigationStrategy classes to solve this problem.

+9
source share

I could not get the desired behavior with the TraverseListener (it would not intersect inside the table), and I could not get it to work with FocusCellManager and CellNavigationStrategy . I finally found this solution that allows me to go from column to column in 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); 
+3
source share

You need to add a KeyListener and set the selection or focus to the following 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"); }} ); 
+1
source share

I also had to implement tabs between elements in a table. We use the grid from the nebula as a table. Firstly, I had to suppress the focus so that it would not leave the table. enter image description here

and then I added a key listener that moves 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 line move it to the beginning of the next line. When the end of the table is reached, the selection returns to the first cell in the table.

This solved the problem for me.

0
source share

All Articles