Despite the fact that the button.doClick() was satisfied with button.doClick() , I was looking for something like what happens after installing the mnemonics, i.e. using button.setMnemonic(KeyEvent.VK_A) . In fact, you can hold ALT + A without any action (other than a visual change). And after releasing key A (with or without ALT), the button starts an ActionEvent.
I found that I can get ButtonModel (see Java 8 API ) using button.getModel() , then visually click the button using model.setPressed(true); model.setArmed(true); model.setPressed(true); model.setArmed(true); (both changed by mnemonics) and visually release the button by setting both values ββto false . And when model.setPressed(false) is called, when the button is pressed and armed, the button automatically launches an ActionEvent (calling model.setArmed(false) only visually changes the button).
[Quote from the Java API documentation ButtonModel] A button is launched, and an ActionEvent is activated when the mouse is released while arming the model [...]
To make the application respond to keystrokes when the button is visible (without the contents of the window or the button that should be the focus owner, that is, when another component in the window is focused), I used key bindings (see the Official Java Guide ).
Work code: Press SHIFT + A to visually press the button (as opposed to pressing ALT with the key after the mnemonic is set using button.setMnemonic() ). And release the key to print the action command ("button") on the console.
// MnemonicCode.java import javax.swing.*; import java.awt.event.*; public class MnemonicCode extends JFrame { public MnemonicCode(int keyCode) { JButton button = new JButton("button"); getContentPane().add(button); addMnemonicToButton(button,keyCode); button.addActionListener(new ActionListener () { public void actionPerformed(ActionEvent e) { System.out.println(e.getActionCommand()); } }); pack(); setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); setVisible(true); } public static void main(String[] args) throws Exception { MnemonicCode bp = new MnemonicCode(KeyEvent.VK_A); } void addMnemonicToButton(JButton button,int keyCode) { int shiftMask = InputEvent.SHIFT_DOWN_MASK; // signature: getKeyStroke(int keyCode, int modifiers, boolean onKeyRelease) KeyStroke keyPress = KeyStroke.getKeyStroke(keyCode,shiftMask,false); KeyStroke keyReleaseWithShift = KeyStroke.getKeyStroke(keyCode,shiftMask,true); // get maps for key bindings InputMap inputMap = button.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW); ActionMap actionMap = button.getActionMap(); // add key bindings for pressing and releasing the button inputMap.put(keyPress,"press"+keyCode); actionMap.put("press"+keyCode, new ButtonPress(button)); inputMap.put(keyReleaseWithShift,"releaseWithShift"+keyCode); actionMap.put("releaseWithShift"+keyCode, new ButtonRelease(button)); ///* // add key binding for releasing SHIFT before A // if you use more than one modifier it gets really messy KeyStroke keyReleaseAfterShift = KeyStroke.getKeyStroke(keyCode,0,true); inputMap.put(keyReleaseAfterShift,"releaseAfterShift"+keyCode); actionMap.put("releaseAfterShift"+keyCode, new ButtonRelease(button)); //*/ } class ButtonPress extends AbstractAction { private JButton button; private ButtonModel model; ButtonPress(JButton button) { this.button = button; this.model = button.getModel(); } public void actionPerformed(ActionEvent e) { // visually press the button model.setPressed(true); model.setArmed(true); button.requestFocusInWindow(); } } class ButtonRelease extends AbstractAction { private ButtonModel model; ButtonRelease(JButton button) { this.model = button.getModel(); } public void actionPerformed(ActionEvent e) { if (model.isPressed()) { // visually release the button // setPressed(false) also makes the button fire an ActionEvent model.setPressed(false); model.setArmed(false); } } } }
mkdrive2 May 23 '14 at 15:17 2014-05-23 15:17
source share