I was reading this question: Action Buttons css style in JavaFX ControlFX dialog, to find out whether it's possible to change JavaFX alert dialogs in terms of style.
The answer was pretty good but I was wondering whether it's possible to just format certain words in the dialog?
For example, I just want one word to be underlined, I can't figure out how to do so though, because there is just one .content.label
and the JavaFX Text
class does not work properly with dialogs (if I'm not wrong).
Below is the code piece how the entire Alert
dialog got modified.
@Override
public void start(Stage primaryStage) {
Alert alert = new Alert(AlertType.CONFIRMATION);
alert.setTitle("Confirmation Dialog");
alert.setHeaderText("This is a Custom Confirmation Dialog");
alert.setContentText("We override the style classes of dialog.css");
Button button = new Button("Click to display an alert");
button.setOnAction(e->{
Optional<ButtonType> result = alert.showAndWait();
result.ifPresent(System.out::println);
});
Scene scene = new Scene(new StackPane(button), 300, 200);
primaryStage.setScene(scene);
primaryStage.show();
DialogPane dialogPane = alert.getDialogPane();
dialogPane.setStyle("-fx-background-color: greenyellow;");
dialogPane.getStyleClass().remove("alert");
GridPane grid = (GridPane)dialogPane.lookup(".header-panel");
grid.setStyle("-fx-background-color: cadetblue; "
+ "-fx-font-style: italic;");
StackPane stackPane = new StackPane(new ImageView(
new Image(getClass().getResourceAsStream("lock24.png"))));
stackPane.setPrefSize(24, 24);
stackPane.setAlignment(Pos.CENTER);
dialogPane.setGraphic(stackPane);
dialogPane.lookup(".content.label").setStyle("-fx-font-size: 16px; "
+ "-fx-font-weight: bold; -fx-fill: blue;");
ButtonBar buttonBar = (ButtonBar)alert.getDialogPane().lookup(".button-bar");
buttonBar.setStyle("-fx-font-size: 24px;"
+ "-fx-background-color: indianred;");
buttonBar.getButtons().forEach(b->b.setStyle("-fx-font-family: \"Andalus\";"));
}