开发者

swing JTable: tweak "enter" so it acts the same as double-clicking a row

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 doubl开发者_如何学编程e-click. But there must be a better way, I think....


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜