Alex Gittemeier
Alex Gittemeier

Reputation: 5373

How to set up JOptionPane to have a default button and a different focused button

In many native OS X applications, there are dialog boxes that have a default option (in blue) and a different option (in outlined blue) that are initially set. This allows a user to use enter and space, respectively, to choose between two different common options.

3 option dialog box in OS X with a focused option, and unfocused option, and a default option

I want to be able to replicate this in a java application. Using JOptionPane in the following way produces something close, but the default option (supplied as the initialValue parameter) is both focused and the default; The blue outline (and the button activated via the spacebar) should be on the left-most button, but it isn't.

int showOptionPane(JFrame parent, String file) {
    String[] selections = {"Save", "Cancel", "Don't Save"};
    return JOptionPane.showOptionDialog(parent,
            "Do you want to save changes to: " + file + "?",
            "Save Work?",
            JOptionPane.YES_NO_CANCEL_OPTION,
            JOptionPane.PLAIN_MESSAGE,
            null, selections, selections[0]);
}

3 option dialog box in OS X with two unfocused options, and a default option that is also focused

I have tried pulling out the source for JOptionPane.showOptionDialog in an attempt to tweak which button got its focus when displayed to the user, but I was having trouble getting at the buttons themselves (since I think those are specified at the pluggable-look-and-feel). How do I get a button focused that is distinct from the default button?

Upvotes: 1

Views: 981

Answers (0)

Related Questions