principal-ideal-domain principal-ideal-domain - 8 months ago 16
Java Question

Remove Enter key binding in JTable

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.


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 = 
//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

                             .put(KeyStroke.‌​getKeyStroke("ENTER"), "none");