在 Java 中将加速键击键传递到主菜单
我使用 MenuItem.setAccelerator() 在主菜单中添加了一些加速器。只是基本的东西,例如用于复制的 ctrl-c 等。
这可以正常工作。但该应用程序有点像 IDE,它有几个包含 JTable 的面板。如果表格单元格具有焦点,它会吸收加速键,这意味着主菜单永远看不到它。
显然,如果可编辑表格单元格处于活动状态,我希望剪切和粘贴键能够正常工作,但在其他情况下,我希望主菜单做出响应。
有什么想法吗?
I have added some accelerators to the main menu, using MenuItem.setAccelerator(). Just basic stuff like ctrl-c for copy, etc.
This works ok. But the app is a bit like an IDE, it has several panels containing JTables. If a table cell has focus, it absorbs the accelerator key, which means the main menu never sees it.
Clearly, if an editable table cell is active I would like the cut and paste keys to function normally, but in every other case I would like the main menu to respond.
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
按键首先转到具有焦点的组件。由于 JTable 将 Ctrl+C 绑定到某个操作,因此会调用该操作。
如果您不喜欢表的默认操作,则需要从表中删除绑定。
阅读 Swing 教程中关于如何使用键绑定。它向您展示了如何删除绑定。
KeyStrokes go to the component that has focus first. Since JTable binds Ctrl+C to an Action, that action is invoked.
If you don't like the default Action of the table, then you would need to remove the binding from the table.
Read the section from the Swing tutorial on How to Use Key Bindings. It shows you how to remove a binding.
谢谢,这让我走上了正轨。
删除绑定并没有完全起作用,它只是停止了表执行其默认操作,因此按键被完全忽略。
但是,将其添加到表本身可以正常工作:(
当然,对每个所需的键重复)。需要与主菜单本身的任何更改保持同步,但我看不到用任何方法来避免这种情况的方法。
Thanks, that got me on the right track.
Removing the bindings didn't quite work, it just stopped the table doing its default action so the keypress was ignored altogether.
However, adding this to the table itself worked ok:
(Repeated for each desired key of course). Needs to be kept in synch with any changes to the main menu itself, but I can't see a way to avoid that with any method.