JTable enter key

The default Key Binding for Enter is the selectNextRowCell action in the table’s WHEN_ANCESTOR_OF_FOCUSED_COMPONENT input map. You can substitute your own action, as outlined below.

private static final String solve = "Solve";
KeyStroke enter = KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0);
table.getInputMap(JTable.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(enter, solve);
table.getActionMap().put(solve, new EnterAction());
...
private class EnterAction extends AbstractAction {

    @Override
    public void actionPerformed(ActionEvent e) {
        ...
    }
}

See also Keyboard Bindings in Swing (mirrored at web.archive.org).

Addendum: You can find more examples here, here and here; the last one is JTable specific.

Leave a Comment