swing JTable:调整“输入”所以它的作用与双击一行相同

发布于 2024-08-20 01:40:04 字数 215 浏览 5 评论 0原文

我脑子抽筋了——如何设置 JTable 以便回车键的作用与双击一行相同?

“好的”强力方法是调用 JTable.getInputMap()JTable.getActionMap() 并添加新的操作名称和新的 Action code> 执行双击操作。但我想一定有更好的方法......

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....

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

〆一缕阳光ご 2024-08-27 01:40:04

您的“暴力”方式是常态,Peter Lang 的 KeyListener 是一种替代方式。来自 Sun 关于按键绑定的讨论

键绑定的替代方法是使用键侦听器。键侦听器作为键盘输入的低级接口占有一席之地,但对于响应各个键,键绑定更合适,并且往往会导致更容易维护的代码。如果要在组件没有焦点时激活按键绑定,那么按键侦听器也会很困难。键绑定的一些优点是它们在某种程度上是自记录的,考虑了包含层次结构,鼓励可重用的代码块(操作对象),并允许轻松删除、自定义或共享操作。此外,它们还可以轻松更改操作所绑定的键。操作的另一个优点是它们具有启用状态,这提供了一种禁用操作的简单方法,而无需跟踪它附加到哪个组件。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文