Skip to main content

Set up the Default and Cancel button

2 replies [Last post]
jacek
Offline
Joined: 2003-08-22
Points: 0

One of the things that bothers me most in many Swing apps I see is the lack of a Default key (reacting to Enter being pressed) and a Cancel key (reacting to ESC being pressed)in
a dialog.

I've tried searching google and there seem to be different opinions on how to implement this. Can anyone recommend the best way to do this?

Also, is there any standard way to put a thicker border around the button that is flagged as Default, just like all regular Windows applications do?

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
shan-man
Offline
Joined: 2006-02-17
Points: 0

Hi Jacek,

> One of the things that bothers me most in many Swing
> apps I see is the lack of a Default key (reacting to
> Enter being pressed) and a Cancel key (reacting to
> ESC being pressed)in
> a dialog.
>

Take a look at JOptionPane. It allows you to set up a default button and it handles ESC. Take note however that in the Windows Look and Feel, the default button follows the focus, as it does on native Windows.

> I've tried searching google and there seem to be
> different opinions on how to implement this. Can
> anyone recommend the best way to do this?
>
> Also, is there any standard way to put a thicker
> border around the button that is flagged as Default,
> just like all regular Windows applications do?

Yes, this happens automatically when you set the button as the default. See JRootPane.setDefaultButton(JButton).

Regards,
Shannon

kleopatra
Offline
Joined: 2003-06-11
Points: 0

Hi Shannon,

>
> Take a look at JOptionPane. It allows you to set up a
> default button and it handles ESC.

Thanks for the pointer, incidentally the issue was on my to-find-out list as well :-)

Just to make it a bit more precise: the magic is implemented in the MultiplexingTextField of BasicOptionPaneUI.

And getting greedy: how about a general mechanism in the inputMap framework to allow multiplexing keybindings for any JComponent?

Greetings
Jeanette