Jacks
Jacks

Reputation: 587

Add text under buttons in Dialog

I was wondering if there is a way to display some text (like a info) under the buttons of a Dialog ? I've looked in many places, but even to align the buttons seems messy (from this post).

This is what I got for now. I just want the "Set my choice ..." text under the 2 buttons.

Dialog simple

I looked for a function in the documentation that could help me display the way I want (like "getButtonBar()" or something like that) with no chance. Also creating a new ButtonBar seems a bit complicated for what I want to achieve.

I also tried to create a stage that could look like a dialog, but I needed the result incoming from clicking "Yes / No" in the same way the Dialogs do.

Is there any way to achieve want I want ? Or do I have to build it completely myself ? Thanks !

Upvotes: 2

Views: 1979

Answers (1)

James_D
James_D

Reputation: 209299

Just override the createButtonBar() method of DialogPane:

DialogPane pane = new DialogPane() {
    @Override
    public Node createButtonBar() {
        VBox vbox = new VBox(5);
        vbox.setAlignment(Pos.BOTTOM_RIGHT);
        vbox.setPadding(new Insets(5));
        vbox.getChildren().add(super.createButtonBar());
        vbox.getChildren().add(new Label("Additional text"));
        return vbox ;
    }
};

Here's a SSCCE:

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ButtonType;
import javafx.scene.control.CheckBox;
import javafx.scene.control.Dialog;
import javafx.scene.control.DialogPane;
import javafx.scene.control.Label;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class CustomDialogPaneTest extends Application {

    @Override
    public void start(Stage primaryStage) {
        Button button = new Button("Show Dialog");
        button.setOnAction(e -> {
            DialogPane pane = new DialogPane() {
                @Override
                public Node createButtonBar() {
                    VBox vbox = new VBox(5);
                    vbox.setAlignment(Pos.BOTTOM_RIGHT);
                    vbox.setPadding(new Insets(5));
                    vbox.getChildren().add(super.createButtonBar());
                    vbox.getChildren().add(new Label("Additional text"));
                    return vbox ;
                }
            };

            CheckBox checkBox = new CheckBox("A check box");
            pane.setContent(checkBox);
            pane.setHeaderText("The header");
            pane.getButtonTypes().addAll(ButtonType.YES, ButtonType.NO);

            Dialog<ButtonType> dialog = new Dialog<>();
            dialog.setDialogPane(pane);
            dialog.showAndWait().ifPresent(System.out::println);
        });

        StackPane root = new StackPane(button);
        root.setPadding(new Insets(20));
        primaryStage.setScene(new Scene(root));
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}

enter image description here

Upvotes: 6

Related Questions