JTable enter key

后端 未结 2 568
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-11-30 09:12

I am developing an application using jTable for inventory management.

The action is, by typing the item code in a jTextField and by pressin

2条回答
  •  难免孤独
    2020-11-30 10:04

    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.

提交回复
热议问题