I am looking for a way to set a specific font for the javafx.scene.control.Alert "OK" button for AlertType = WARNING. Does anyone know whether this is possible and, if so, how this is done?
Thanks for any advice.
I am looking for a way to set a specific font for the javafx.scene.control.Alert "OK" button for AlertType = WARNING. Does anyone know whether this is possible and, if so, how this is done?
Thanks for any advice.
As Slaw wrote in his comment there is a lookupButton
method available:
This method provides a way in which developers may retrieve the actual Node for a given ButtonType (assuming it is part of the button types list).
So you can simply use it like this to set a new font:
alert.getDialogPane().lookupButton(ButtonType.OK).setStyle("-fx-font-family: Vijaya;");
or you can do:
((Button) alert.getDialogPane().lookupButton(ButtonType.OK))
.setFont(new Font("Vijaya", 12));
Edit (answer to a question in the comment section):
If you want to use a dedicated css file, you can do it like this:
Alert alert = new Alert(Alert.AlertType.WARNING);
DialogPane dialogPane = alert.getDialogPane();
// because the alert is a new stage, you have to reference the style sheet you want to use:
dialogPane.getStylesheets().add(App.class.getResource("alerts.css").toExternalForm());
// find the button and give it an id:
Button okBtn = (Button) dialogPane.lookupButton(ButtonType.OK);
okBtn.setId("ok-btn");
alerts.css:
#ok-btn {
-fx-font-family: Vijaya;
}
If you want to style not only the button, you can have a look at this answer.