如何为 JTree/JList/JTable 设置助记符
它们不像按钮那样具有 setMnemonic()。
我正在尝试构建一些自动化的 UI 测试。使整个 UI 键盘驱动对此至关重要。使用助记符(加速器或快捷方式)在小部件之间移动是一种经过验证的正确方法。
但我似乎不知道如何为上述组件设置助记符。 有多种方法可以强制键盘导航、元素之间的选项卡或手动注册全局加速器。但如果我必须走那么远,我至少想要一些关于最佳实践的意见。
<编辑>
camickr 是对的。我只需要放松并读完有关该主题的文档即可。然后事情就变得很简单了。这是任何进行搜索的人的最终结果。
treeWidget
.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW)
.put(KeyStroke.getKeyStroke(LIST_MN,InputEvent.ALT_DOWN_MASK), "focus_jtree");
treeWidget
.getActionMap()
.put("focus_jtree", new AbstractAction() {
@Override
public void actionPerformed(ActionEvent event) {
treeWidget.requestFocusInWindow();
}});
They don't have setMnemonic(), as Buttons do.
I'm trying to build some automated UI testing. Making the entire UI keyboard driven is vital for this. Using mnemonics (accelerators or shortcuts) to move between widgets is a tried and true method.
I can't seem to figure out how to set a mnemonic for the above components though.
There are ways to brute-force the keyboard navigation, tabbing between elements or manually register global accelerators. But if I have to go that far I'd at least like some opinions on the best practice for this.
<edit>
camickr was right. I just had to relax and finish reading the docs on the subject. then it became quite simple. Here's is the final result for anyone doing a search.
treeWidget
.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW)
.put(KeyStroke.getKeyStroke(LIST_MN,InputEvent.ALT_DOWN_MASK), "focus_jtree");
treeWidget
.getActionMap()
.put("focus_jtree", new AbstractAction() {
@Override
public void actionPerformed(ActionEvent event) {
treeWidget.requestFocusInWindow();
}});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
请参阅如何使用键绑定中的 Swing 教程。
不知道你的意思是什么。您可以通过 Tab 键切换到任何这些组件。
See the Swing tutorial in How to Use Key Bindings.
Not sure what you mean by that. You can tab to any of those components.
我当前的解决方案是
似乎可以完成这项工作,但我的小部件并不总是有标签。所以我只是添加标签来让它们更容易助记。而且标签通常不包含助记符。
My current solution is
Seems to do the job, but my widgets don't always have labels. So I'm just adding labels to give them easy mnemonics. And the labels often don't contain the mnemonic character anyways.