2

What is the simplest and fastest way to remove the standard enter key bindings (pressing enter selects the next row) in a JTable?

That's what I tried

table.getInputMap().put(KeyStroke.getKeyStroke("ENTER"), null);

But it doesn't work. I assume that we have to do that somehow for each cell and not the table itself.

Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
principal-ideal-domain
  • 3,998
  • 8
  • 36
  • 73

3 Answers3

4

JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT and JComponent.WHEN_IN_FOCUSED_WINDOW have a value for the enter keystroke. So you want to get both of them

Correction: You need to get the InputMap for WHEN_ANCESTOR_OF_FOCUSED_COMPONENT

InputMap iMap1 = 
         table.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
//InputMap iMap2 = 
        // table.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);

Then you want to set the value for the map to "none", instead of null, as described in How to Use Key Bindings.

To make a component ignore a key that it normally responds to, you can use the special action name "none". For example, the following code makes a component ignore the F2 key.

component.getInputMap().put(KeyStroke.getKeyStroke("F2"), "none");

So just do:

KeyStroke stroke = KeyStroke.getKeyStroke("ENTER");
iMap1.put(stroke, "none");
//iMap2.put(stroke, "none");

Also note when you just do getInputMap() without any arguments, it's basically the same thing as getInputMap(JComponent.WHEN_FOCUSED). And in the case of JTable, there's no value for the enter keystroke for that InputMap.

Read more at How to Use Key Bindings. You'll get a better explanation of the different InputMaps


UPDATE : Correction (corrections made above either struck through or // commented out)

You only to set it for the InputMap for JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT


UPDATE per the OP comment: Yes in short

table.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT)
                             .put(KeyStroke.‌​getKeyStroke("ENTER"), "none");
Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
1

This seems to be the most convenient way:

table.registerKeyboardAction(
    null,
    KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0),
    JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT
);

To assign an action replace null with ActionListener call or with e -> someMethod() for JDK 1.8

Oleg Mikhailov
  • 5,751
  • 4
  • 46
  • 54
0

Update:
David Kroukamp solution:

private void createKeybindings(JTable table) {
table.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), "Enter");
    table.getActionMap().put("Enter", new AbstractAction() {
        @Override
        public void actionPerformed(ActionEvent ae) {}
    });
}

And for you should be enough:

table.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), "Enter");
table.getActionMap().put("Enter",null);

I dont know if it is possible to use null, you could use anonymous class instead...

maskacovnik
  • 3,080
  • 5
  • 20
  • 26