It took ages to work out how to do this, so I decided to post it here in the hope someone else will benefit. Here's how to install a keyboard shortcut to a Swing JButton so the button behaves as though it was clicked when the key is pressed.
- inputMap.put(enter, "ENTER");
- saveButton.getActionMap().put("ENTER",
- new ClickAction(saveButton));
-
- ...
-
-
- this.button = button;
- }
-
- button.doClick();
- }
- }
-
A complete example is in shortcut-buttons-src-0.1.jar. The download includes an ant build file. To compile and run, just type "ant run" at the shell prompt. You will need ant and a JDK >= 1.5 installed.
Source download: shortcut-buttons-src-0.1.jar