Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 504
Oleg Beat Avatar asked May 01 '12 08:05

Oleg Beat


People also ask

How do you make a new line in JOptionPane?

You have to use \n to break the string in different lines. Or you can: Another way to accomplish this task is to subclass the JOptionPane class and override the getMaxCharactersPerLineCount so that it returns the number of characters that you want to represent as the maximum for one line of text.

What are the 4 JOptionPane dialog boxes?

The JOptionPane displays the dialog boxes with one of the four standard icons (question, information, warning, and error) or the custom icons specified by the user.


1 Answers

This will make the uimanager handle the focused button. This will allow you to use both enter or space to select the focused button.

UIManager.put("Button.defaultButtonFollowsFocus", Boolean.TRUE);
like image 50
Mitch Connor Avatar answered Oct 04 '22 15:10

Mitch Connor