Since jdk 8u40, I\'m using the new javafx.scene.control.Alert
API to display a confirmation dialog. In the example below, \"Yes\" button is focused by default inste
If you have a look at (private) ButtonBarSkin
class, there is a method called doButtonOrderLayout()
that performs the layout of the buttons, based in some default OS behavior.
Inside of it, you can read this:
/* now that all buttons have been placed, we need to ensure focus is set on the correct button. [...] If so, we request focus onto this default button. */
Since ButtonType.YES
is the default button, it will be the one focused.
So @ymene answer is correct: you can change the default behavior and then the one focused will be NO
.
Or you can just avoid using that method, by setting BUTTON_ORDER_NONE
in the buttonOrderProperty()
. Now the first button will have the focus, so you need to place first the NO
button.
alert.getButtonTypes().setAll(ButtonType.NO, ButtonType.YES);
ButtonBar buttonBar=(ButtonBar)alert.getDialogPane().lookup(".button-bar");
buttonBar.setButtonOrder(ButtonBar.BUTTON_ORDER_NONE);
Note that YES
will still have the default behavior: This means NO
can be selected with the space bar (focused button), while YES
will be selected if you press enter (default button).
Or you can change also the default behavior following @crusam answer.