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();
}});
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.