Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 712
Andez Avatar asked Dec 21 '22 18:12

Andez


1 Answers

There is another simpler possibility: you can press alt in a program and then release it in a 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 you call:

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

the behavior is exactly the same, as you desired...

like image 146
v.mic Avatar answered Dec 24 '22 08:12

v.mic