JButtons consider pressing the space bar to be the same as clicking on the JButton (assuming the JButton has the focus, which I am assuming here). Is there a way to turn off this behavior so they ignore pressing the space bar?
Also, more generally, is there a technique for doing this with AbstractButtons
?
startButton. setEnabled(false); stopButton. setEnabled(true);
We use an Event Listener to listen for certain events and edit the GUI on the occurrence of the event. There are many different Event Listeners, each looking out for a specific type of event. The Event Listener a JButton uses is an ActionListener.
Swing defines four types of buttons: JButton, JToggleButton, JCheckBox, and JRadioButton. All are subclasses of the AbstractButton class, which extends JComponent. Thus, all buttons share a set of common traits. The Swing Buttons. Swing defines four types of buttons: JButton, JToggleButton, JCheckBox, and JRadioButton.
The link given by aioobe shows how to do it for an individual button. If you want to do this for all JButton's you would do:
InputMap im = (InputMap)UIManager.get("Button.focusInputMap");
im.put(KeyStroke.getKeyStroke("pressed SPACE"), "none");
im.put(KeyStroke.getKeyStroke("released SPACE"), "none");
If you want to do it for check boxes and radio buttons you would need to repeat the above for each different input map.
You may want to read Enter Key and Button. It actually does the opposite of what you want since it explains how to invoke the button when the Enter key is used. But it may help explain why this solution works in reverse.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With