Reputation: 977
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
The problem I am facing is that regardless in what order I specify the ButtonType
s 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?
Upvotes: 4
Views: 3706
Reputation:
This is a modified version of @Adam solution. Advantage - no need in extra class, no need to remove existing dialog pane with all its content.
ButtonBar buttonBar = (ButtonBar) alert.getDialogPane().lookup(".button-bar");
buttonBar.setButtonOrder(ButtonBar.BUTTON_ORDER_NONE);
Upvotes: 1
Reputation: 36723
Example if you really need to specify button order...
private static class FixedOrderButtonDialog extends DialogPane {
@Override
protected Node createButtonBar() {
ButtonBar node = (ButtonBar) super.createButtonBar();
node.setButtonOrder(ButtonBar.BUTTON_ORDER_NONE);
return node;
}
}
And then when you create your Alert.
alert.setDialogPane(new FixedOrderButtonDialog());
Upvotes: 5
Reputation: 159486
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).
Upvotes: 2