Search code examples
user-interfacejavafx-8alert

How to specify a particular order of buttons in a JavaFX 8 Alert


I need to ask the user to confirm doing an action. The set of buttons of the confirmation dialog are "Yes", "No" and "Cancel". My code is below:

private ButtonType askYesNoCancel(String question) {
    Alert alert = new Alert(AlertType.CONFIRMATION);
    alert.setContentText(question);
    alert.getButtonTypes().setAll(ButtonType.YES, 
                                  ButtonType.NO, 
                                  ButtonType.CANCEL);
    return alert.showAndWait().get();
}

which gives me

Asking Yes/No/Cancel confirmation

The problem I am facing is that regardless in what order I specify the ButtonTypes for an Alert, I get the same button order (No - Cancel - Yes). I took a look at API documentation for Alert, Button and ButtonType, alas, not to find an answer.

What I try to accomplish, is the button order Yes - No - Cancel. Any advice?


Solution

  • Check the answer to: Enter Key Event Is Not Working On Dialog In Javafx?, though I guess that answer more appropriately belongs with this question and perhaps doesn't fully explain what is going on. Also read the ButtonBar javadoc.

    Basically, the button position is determined by a combination of the ButtonType of each button and the ButtonBar's buttonOrder. By changing either of these things, you will end up with different button layouts.

    As I recall, customizing the ButtonBar was kind of tricky. You might need to subclass the alert and override createButtonBar.

    What I try to accomplish, is the button order Yes - No - Cancel. Any advice?

    My advice is: Don't try to customize the button order, but let the system default order be applied. The button order has already been preconfigured to match the standard button layout for dialogs for various operating systems. Reordering the buttons to deviate from that standard may make your application just slightly more confusing for users (that's a pretty subjective opinion though).