tags:

views:

283

answers:

1

I'm having a brain cramp -- how do I set up a JTable so that the enter key acts the same as double-clicking a row?

The "nice" brute force way is to call JTable.getInputMap() and JTable.getActionMap() and add a new action name and a new Action that does a double-click. But there must be a better way, I think....

+1  A: 

Your "brute force" way is the norm, with Peter Lang's KeyListener being an alternative. From Sun's discussion on Key Bindings:

An alternative to key bindings is using key listeners. Key listeners have their place as a low-level interface to keyboard input, but for responding to individual keys key bindings are more appropriate and tend to result in more easily maintained code. Key listeners are also difficult if the key binding is to be active when the component doesn't have focus. Some of the advantages of key bindings are they're somewhat self documenting, take the containment hierarchy into account, encourage reusable chunks of code (Action objects), and allow actions to be easily removed, customized, or shared. Also, they make it easy to change the key to which an action is bound. Another advantage of Actions is that they have an enabled state which provides an easy way to disable the action without having to track which component it is attached to.

akf