3

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\";"));
}
Community
  • 1
  • 1
LOLWTFasdasd asdad
  • 2,625
  • 5
  • 26
  • 39

1 Answers1

5

If you have a look at DialogPane class, you'll see that by default it uses a Label for the content. But also you have this:

/**
 * Assign dialog content. Any Node can be used
 * 
 * @param content
 *            dialog's content
 */
public final void setContent(Node content) {
    this.content.setValue(content);
}

So all you need to do is provide your custom node for the content:

Text text1=new Text("This is ");
text1.setStyle("-fx-font-size: 16px; -fx-fill: blue;");
Text text2=new Text("underlined ");
text2.setStyle("-fx-font-size: 16px; -fx-fill: blue; -fx-underline: true;");
Text text3=new Text("text");
text3.setStyle("-fx-font-size: 16px; -fx-fill: blue;");

TextFlow flow = new TextFlow(text1,text2,text3);

dialogPane.setContent(flow);

and you'll have your dialog:

Dialog

José Pereda
  • 44,311
  • 7
  • 104
  • 132