Java JOptionPane.showConfirmDialog hotkey

I am showing a confirmation dialog in Java using JOptionPane.showConfirmDialog. The dialog box displays a Yes No message for the user. This is called the following:

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

The question is, is this a simple confirmation, can the user press y for yes and n for no? Currently the user needs to click on the buttons?

Thanks,

Andez

+4
source share
4 answers

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

You can also press Tab to switch between them and Space to press.

+2
source

There is another simpler option: you can press alt in the program and then release it the same way:

 public static void pressAlt(){ try { Robot r = new Robot(); r.keyPress(KeyEvent.VK_ALT); } catch (AWTException ex) { Logger.getLogger(ManualDetection.class.getName()).log(Level.SEVERE, null, ex); } } public static void releaseAlt(){ try { Robot r = new Robot(); r.keyRelease(KeyEvent.VK_ALT); } catch (AWTException ex) { Logger.getLogger(ManualDetection.class.getName()).log(Level.SEVERE, null, ex); } } 

then when calling:

  pressAlt(); int confirm = JOptionPane.showConfirmDialog(... bla bla bla ... ,JOptionPane.YES_NO_OPTION); releaseAlt(); 

the behavior is exactly the same as you wanted ...

+6
source

No, but you can create your own JDialog that could do this.

+2
source

Source: https://habr.com/ru/post/1336960/


All Articles