Search code examples
javaswingjoptionpane

JOptionPane.YES_OPTION == ENTER on each button


I have a option dialog like this:

String[]  options = ["Yes", "No"]; //button names

int n = JOptionPane.showOptionDialog(singleFrameService.getFrame(),
        "Some Question?",
        "",
        JOptionPane.YES_NO_OPTION,
        JOptionPane.QUESTION_MESSAGE,
        null,     //do not use a custom Icon
        options,  //the titles of buttons
        options[0]); //default button title

//if press yes
if (n == JOptionPane.YES_OPTION){
    //make some if pressed Yes
}

When I used mouse and press Yes/No - all work fine... But when I start use keyboard, press TAB to go to "No" button, and then press ENTER - work "Yes" option


Solution

  • It all depends on the look 'n feel, AFAIK. In your L&F, "Enter" means "press the default button" (which is Yes). Pressing the focused button is probably done by pressing the space bar.