Change focus to next component in JTable using TAB

我只是一个虾纸丫 提交于 2019-12-18 07:44:41

问题


JTable's default behavior is changing focus to next cell and I want to force it to move focus to next component (e.g. JTextField) on TAB key pressed.
I overrided isCellEditable method of DefaultTableModel to always return false:

public boolean isCellEditable(int rowIndex, int columnIndex) {
    return false;
}

But it still doesn't change focus to next component!
How should I make JTable change focus to next component instead of next cell?


回答1:


If you really want this, you need to change the default behavior of the tables action map.

ActionMap am = table.getActionMap();
am.put("selectPreviousColumnCell", new PreviousFocusHandler());    
am.put("selectNextColumnCell", new NextFocusHandler());    

Then you need a couple of actions to handle the traversal

public class PreviousFocusHandler extends AbstractAction {
    public void actionPerformed(ActionEvent evt) {
        KeyboardFocusManager manager = KeyboardFocusManager.getCurrentKeyboardFocusManager();
        manager.focusPreviousComponent();
    }
}

public class NextFocusHandler extends AbstractAction {
    public void actionPerformed(ActionEvent evt) {
        KeyboardFocusManager manager = KeyboardFocusManager.getCurrentKeyboardFocusManager();
        manager.focusNextComponent();
    }
}

Another approach would be to disable the underlying Action...

ActionMap am = table.getActionMap();
am.get("selectPreviousColumnCell").setEnabled(false);
am.get("selectNextColumnCell").setEnabled(false);

(haven't tested this)

The benefit of this approach is can enable/disable the behaviour as you need it without needing to maintain a reference to the old Actions




回答2:


The shift-/tab keys are used by default for transfering focus between components. JTable explicitly requests to handle the shift-/tab internally (by providing sets of focusTraversalKeys which doesn't include those).

Following the general rule (if there's specilized api available for a task, use that instead of rolling your own), the solution is to set traversal keys to again contain them:

Set<AWTKeyStroke> forward = new HashSet<AWTKeyStroke>(
        table.getFocusTraversalKeys(KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS));
forward.add(KeyStroke.getKeyStroke("TAB"));
table.setFocusTraversalKeys(KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS, forward);
Set<AWTKeyStroke> backward = new HashSet<AWTKeyStroke>(
        table.getFocusTraversalKeys(KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS));
backward.add(KeyStroke.getKeyStroke("shift TAB"));
table.setFocusTraversalKeys(KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS, backward);



回答3:


  • default (implemented KeyBinding for JTable) is about next cell and from last cell to first,

  • you can to remove KeyBindings by setting to the null value




回答4:


To reset to the standard keyboard bindings (typically TAB and SHIFT+TAB), simply specify null for the keystrokes parameter to Component.setFocusTraversalKeys:

table.setFocusTraversalKeys(KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS, null);
table.setFocusTraversalKeys(KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS, null);


来源:https://stackoverflow.com/questions/12154734/change-focus-to-next-component-in-jtable-using-tab

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!