如何为 JTree/JList/JTable 设置助记符

发布于 2024-10-19 10:36:05 字数 738 浏览 8 评论 0原文

它们不像按钮那样具有 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 技术交流群。

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

发布评论

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

评论(2

鹤仙姿 2024-10-26 10:36:05

请参阅如何使用键绑定中的 Swing 教程。

但我似乎无法弄清楚如何仅使用键盘事件将焦点设置到这些组件之一。

不知道你的意思是什么。您可以通过 Tab 键切换到任何这些组件。

See the Swing tutorial in How to Use Key Bindings.

But I can't seem to figure out how to set focus to one of these components using only keyboard events.

Not sure what you mean by that. You can tab to any of those components.

女中豪杰 2024-10-26 10:36:05

我当前的解决方案是

JLabel jLabel = new JLabel("List");
  jLabel.setDisplayedMnemonic(LIST_MN);
  jLabel.setLabelFor(treeWidget);
  add(jLabel);
  add(treeWidget);

似乎可以完成这项工作,但我的小部件并不总是有标签。所以我只是添加标签来让它们更容易助记。而且标签通常不包含助记符。

My current solution is

JLabel jLabel = new JLabel("List");
  jLabel.setDisplayedMnemonic(LIST_MN);
  jLabel.setLabelFor(treeWidget);
  add(jLabel);
  add(treeWidget);

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.

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