Swing QuickTip: Default Button(21:53, 12. Jan. 2011)

As you maybe know Swing allows you to specify a default button which can be selected by a special key - even if the button isn't owner of the focus. The related button is passed to the JRootPane by calling JRootPane#setDefaultButton(JButton b) just like below.

JButton button = new JButton("My Default Buttton");
frame.setDefaultButton(button);

Synthetica acts just like Metal LaF which means that the key for pressing a regular button is the 'space'-key and the default button is pressed with the 'enter'-key. In Synthetica you can also specify that the default button follows the focus - so each button can be pressed with Enter/Return:

UIManager.setLookAndFeel(...)
UIManager.put("Button.defaultButtonFollowsFocus", true);

Another way to support pressing buttons by Enter or any other key, is to specify an input map for each supported key, just like in the example below.

UIManager.setLookAndFeel(...);
UIManager.put("Button.focusInputMap", new UIDefaults.LazyInputMap(new Object[]
{
  "SPACE", "pressed",
  "released SPACE", "released",
  "ENTER", "pressed",
  "released ENTER", "released"
}));

Related Links

Swing Tutorial - How to use JButton features