Tab between fields in TableViewer

前端 未结 5 667
广开言路
广开言路 2020-12-19 10:20

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 T         


        
相关标签:
5条回答
  • 2020-12-19 10:27

    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.

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

    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.

    0 讨论(0)
  • 2020-12-19 10:32

    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.

    0 讨论(0)
  • 2020-12-19 10:32

    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");
       }}
    );
    
    0 讨论(0)
  • 2020-12-19 10:42

    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.

    0 讨论(0)
  • 2020-12-19 10:49

    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);
    
    0 讨论(0)
提交回复
热议问题