A JButton that has focus can be activated by using the space bar. However, on any given Window a single button can be designated as the “Default Button” for the Window. The default button is noticeable by the darker border around the button. The default button can be activated by using the Enter key, even when it doesn’t have focus.
To assign an initial default button to a JFrame or JDialog you can use:
getRootPane().setDefaultButton(...);
In Windows Look and Feel, when you tab to a button it temporarily becomes the default button. In this case, you can then activate the button either by using the space bar or the Enter key. When you tab off the button, the original default button is reset. However, in the Metal Look and Feel, the default button never changes. Therefore, even when a button has focus you can still only activate the button using the space bar.
Many users are more familiar with the Windows approach and would like the Metal LAF to work that way as well. That is, they would like to be able to activate a button that has focus with the Enter key. The solution to this problem will depend on the version of Java you are using.
Read the rest of this entry »