Setting Mnemonics and Hot Keys for a JOptionPane Dialog

Posted by Daniel Bingham on Stack Overflow See other posts from Stack Overflow or by Daniel Bingham
Published on 2009-10-01T18:06:52Z Indexed on 2010/04/25 5:03 UTC
Read the original article Hit count: 287

Filed under:
|
|
|
|

Is it possible to assign hotkeys and mnemonics to the buttons in a JOptionPane Dialog? I'd like to be able, in a JOptionPane generated message dialog with the options Yes, No and Cancel, press Y to hit the Yes button, N to hit the No button and escape to activate the escape button. Similarly in a dialog with Okay and Cancel buttons I'd like to be able to activate them with enter and escape.

I've attempted passing JButtons into the JOptionPane's button Object array with the Mnemonics set already. The mnemonics work and the buttons show up correctly in the dialogs, however, they do not act properly when they are activated. Most noticeably they do not dispose of the dialog.

What is the correct way to add hotkeys and Mnemonics to a JOptionPane Dialog's buttons?

As always, my apologies ahead of time if this is a duplicate - I searched both Google and Stackoverflow and found nothing.

© Stack Overflow or respective owner

Related posts about java

Related posts about hotkeys