Search code examples
javaswingshortcutjoptionpane

Java JOptionPane.showConfirmDialog hot key


I am displaying a confirmation dialog in Java using JOptionPane.showConfirmDialog. The dialog shows a Yes No confirmation to the user. This is called as follows:

int result = JOptionPane.showConfirmDialog(sessionObjects.getActiveComponent(), 
                 "Are you sure you want to exit?", "My App", JOptionPane.YES_NO_OPTION);

The question is as this is a simple confirmation, can the user press y for yes and n for no? At present the user has to click on the buttons?

Thanks,

Andez


Solution

  • You already have "hotkeys" (mnemonics) for the buttons: Alt+Y for "Yes" and Alt+N for "No".

    You can also hit Tab to toggle between them and Space to press.