重新定义 JRadioButton 中 Tab 键的行为?

发布于 2024-08-17 22:13:51 字数 524 浏览 2 评论 0原文

我正在尝试重新定义 JRadioButton 中 Tab 键的行为,使其行为类似于其他 GUI 应用程序中的单选按钮,即:

  • 箭头键在 ButtonGroup 中的单选按钮之间循环(我有此工作)
  • Tab 将焦点移至组中最后一个单选按钮之后的下一个组件(问题区域)

我有一个操作,它执行必要的步骤来找到要聚焦的正确组件和所有内容,但向 InputMap 添加条目似乎不起作用:

getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_TAB, 0), "jumpNext");
getActionMap().put("jumpNext", new JumpNextAction());

我的条目Tab 的输入映射似乎被忽略,因为该操作永远不会被执行。我想这可能是因为 KeyboardFocusManager 或相关的东西在到达组件的输入映射之前消耗了 Tab 事件。

关于如何停止这种行为并改用自定义选项卡行为有什么想法吗?

I'm trying to redefine the behavior of the Tab key in a JRadioButton so it behaves like radio buttons in other GUI applications, that is:

  • Arrow keys cycle through the radio buttons in the ButtonGroup (I have this working)
  • Tab moves focus to the next component after the last radio button in the group (Problem area)

I have an Action that performs the necessary steps to find the right component to focus and everything, but adding an entry to the InputMap doesn't seem to work:

getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_TAB, 0), "jumpNext");
getActionMap().put("jumpNext", new JumpNextAction());

My entry in the input map for Tab seems to be ignored, as the action never gets executed. I imagine that this is probably because the KeyboardFocusManager or something related is consuming the Tab event before it gets to the component's input map.

Any ideas on how I can stop this behavior and have my custom Tab behavior instead?

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

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

发布评论

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

评论(1

迷你仙 2024-08-24 22:13:51

由于 KeyboardFocusManager 的原因,它可能无法像您所说的那样到达输入映射,但是如果您在其上放置较低级别的 keyListener,我打赌您可以拦截并消费()该事件,因此 KeyboardFocusManager 不会处理它。

或者,您可以在单选按钮上调用 JComponent.setFocusTraversalKeys() 方法,删除默认的 Tab 键,然后 inputmap 可能会像您尝试的那样处理它。

It may not be getting to the inputmap as you said because of the KeyboardFocusManager, however if you put a lower level keyListener on it I bet you can intercept and consume() the event so the KeyboardFocusManager doesn't handle it.

Alternatively, you may be able to call the JComponent.setFocusTraversalKeys() method on the radio buttons, removing the default tab key and then then inputmap might handle it like you were trying.

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