javafxcontrolsfx

Bold Text Parts in ControlsFX Notification


I'm trying to write bold text inside the body of a Notification.

Notifications.create()
    .title("My Title")
    .text("This is <bold>text</bold>")
    .showInformation();

As far as I can tell there's only the .text("my text") method to set the text of a Notification. However I'd like to use a TextFlow in there to modify certain parts of a text.

I've also tried to use CSS for this but that only lets me change to overall appearances of the Notification as setting specific text through CSS is explicitly not supported.

My question now is: Is there a way to style parts of a text inside a Notification differently?


Solution

  • Got it working now. The solution is to use .graphic(Node node). If you still want to display an Image at the left side you need to put everything in a Pane and add some padding. I yoinked the original image from here.

            BorderPane borderPane = new BorderPane();
        borderPane.setLeft(new ImageView(Controller.class.getResource("/dialog-information.png").toExternalForm()));
        TextFlow textFlow = new TextFlow();
        textFlow.setPadding(new Insets(15, 0, 5, 5));
        Text text1 = new Text("This is ");
        Text text2 = new Text("bold");
        text2.setStyle("-fx-font-weight: bold");
        textFlow.getChildren().addAll(text1, text2);
        borderPane.setRight(textFlow);
        Notifications.create()
                .title("My Title")
                .graphic(borderPane)
                .hideAfter(Duration.seconds(10))
                .show();
    

    It is important to use .show() and not .showInformation/Error/Warning() because that would override the BorderPane.

    Result: example of Notification with TextFlow