Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disabling space bar triggering click for JButton

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?

like image 435
Paul Reiners Avatar asked Dec 17 '10 16:12

Paul Reiners


People also ask

How do you change a button for enable to disable after click in Java?

startButton. setEnabled(false); stopButton. setEnabled(true);

Which listener is implemented for JButton?

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.

What is swing button?

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.


1 Answers

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.

like image 140
camickr Avatar answered Sep 28 '22 16:09

camickr