如何在 JPopupMenu 中获得焦点
我是一名经验丰富的 Java 程序员,但还是一名 Swing 新手,所以请耐心等待。
我希望有一个具有键盘焦点的 JPopupWindow 。我想响应箭头键、Escape(关闭菜单)和 Enter(调用该项目)。我是否必须在菜单中添加一个 KeyListener 并自己编写所有这些代码,或者是否可以设置某种“模式”来激活此行为,这似乎应该是标准的。
JPopupMenus 上的标准 Swing 教程部分仅涉及助记符和加速器。我不是特别想要那些。
很难相信这完全违背了 JPopupMenu 的原则。
实现我想要的东西的最佳方法是什么?
I am an experienced Java programmer, but a Swing newbie so please bear with me.
I wish to have a JPopupWindow which has keyboard focus. I want to respond to arrow keys, escape (to close the menu) and Enter (to invoke the item). Must I add a KeyListener to the menu and code all this myself, or is there some sort of "mode" I can set to activate this behavior which seems like it ought to be standard.
The standard Swing tutorial section on JPopupMenus speaks only of mnemonics and accelerators. I don't particularly want those.
Hard to believe that this is totally against the grain of JPopupMenu.
What is best way to get something like I want implemented?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
一旦你执行
jpopupMenu.show(Component invoker, int x, int y)
,它就会根据你的需要自动响应键盘。如果这不起作用,那么您可能需要解决其他一些问题,即其他一些组件在不应该夺走菜单焦点的情况下夺走了焦点等。Once you do
jpopupMenu.show(Component invoker, int x, int y)
it will automatically respond to keyboard as you want. If this is not working then you might need to fix some other issue i.e some other component is snatching away focus from the menu when it should not etc.我建议添加 < 而不是
KeyListener
em>Action 实例到您的JPopupMenu
。使用setComponentPopupMenu()
将弹出窗口添加到面板中。Instead of
KeyListener
, I'd recommend adding Action instances to yourJPopupMenu
. UsesetComponentPopupMenu()
to add the popup to your panel.