Extend keybindings in JTable

127 Views Asked by At

After adding a binding for F4 to a JTable inside a panel, the standard behavior for TAB stops working. Instead of the expected jump to the next cell, the current cell goes into edit mode.

This happens when using ActionMap/InputMap or adding a KeyListener to the table

ActionMap map = new ActionMap();
map.put("f4Action", new F4Action());
map.setParent(this.getActionMap());

InputMap input = new InputMap();
input.put(KeyStroke.getKeyStroke("F4"), "f4Action");
input.setParent(this.getInputMap());

// this is the table in question
table.setInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, input);
table.setActionMap(map);

or just table.addKeyListener(new KeyListener() { /*...*/ } with the other approach.


Is it possible to keep the standard behavior and only override explicit keybindings? And how can this be done.

1

There are 1 best solutions below

1
On BEST ANSWER
ActionMap map = new ActionMap();

Don't create a new ActionMap.

Just use the existing ActionMap:

ActionMap map = table.getActionMap();

See Key Bindings for a list of all the current bindings and templates of how to customize the bindings.