Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.5k views
in Technique[技术] by (71.8m points)

java - Action Buttons css style in JavaFX ControlFX dialog

I've been using ControlsFX dialogs to show information, but the style of my application is not blue, and does not match dialog style (color, borders) is there a way to change the button color or styles?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Since you don't provide the version you are using, I'll go with the new OpenJFX-Dialogs project (source, started since 8.20.7 version), which are by the way the same dialogs we'll have with JDK8u40.

Firts, let's add some alert dialog:

@Override
public void start(Stage primaryStage) {
    Alert alert = new Alert(AlertType.CONFIRMATION);
    alert.setTitle("Confirmation Dialog");
    alert.setHeaderText("This is a Regular Confirmation Dialog");
    alert.setContentText("This is the message");

    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();
}

Regular Alert Dialog

Now, to style this dialog, we need an instance of DialogPane, which is the root node within a dialog, where the header, the content and the buttons are displayed.

Via lookups or via getChildren() it's easy to find these components.

This is an example of customizing all of the components of the dialog:

@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();
// root
    dialogPane.setStyle("-fx-background-color: greenyellow;");

// 1. Grid
    // remove style to customize header
    dialogPane.getStyleClass().remove("alert");

    GridPane grid = (GridPane)dialogPane.lookup(".header-panel"); 
    grid.setStyle("-fx-background-color: cadetblue; "
            + "-fx-font-style: italic;");

    // custom icon
    StackPane stackPane = new StackPane(new ImageView(
            new Image(getClass().getResourceAsStream("lock24.png"))));
    stackPane.setPrefSize(24, 24);
    stackPane.setAlignment(Pos.CENTER);
    dialogPane.setGraphic(stackPane);

// 2. ContentText with just a Label
    dialogPane.lookup(".content.label").setStyle("-fx-font-size: 16px; "
            + "-fx-font-weight: bold; -fx-fill: blue;");

// 3- ButtonBar
    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";"));

}

And this is how it looks like:

Custom Alert Dialog


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...