JAVAFX Stage : Default button selection and alignment with text padding

2.9k Views Asked by At

I am new to Javafx and trying to implement a Warning/Confirmation Dialog box for the User.

I am able to get the dialog box, but unable to align it as per requirement.

Below is how my dialog box looks currently: enter image description here

Now, I need to make two changes,

  1. I want to have buttons side by side, with yes first and no after that. But No should be selected by default. So that on closing No is sent as response.
  2. I want to have some space between the text and the buttons. Also is it possible to get text in two lines if the text size increases?

Below is my current code:

public static boolean display(String title, String message){
        Stage window = new Stage();
        window.initModality(Modality.APPLICATION_MODAL);
        window.setTitle("Confirmation");
        window.setMinWidth(540);
        window.setMinHeight(250);
        Label label = new Label();
        label.setText(message);

        Button yesButton = new Button("Yes");
        Button noButton = new Button("no");

        yesButton.setOnAction(e->{
            answer = true;
            window.close();
        });

        noButton.setOnAction(e->{
            answer = false;
            window.close();
        });
        
        VBox layout = new VBox(10);
        layout.getChildren().addAll(label,noButton,yesButton);
        layout.setAlignment(Pos.CENTER);
        Scene scene = new Scene(layout);
        window.setScene(scene);
        window.showAndWait();
        return answer;
    }

Please help. Thanks in advance.

2

There are 2 best solutions below

0
On BEST ANSWER

I want to have buttons side by side, with yes first and no after that

Put the buttons into a HBox

No should be selected by default. So that on closing No is sent as response.

See here for a similar question

I want to have some space between the text and the buttons.

Add some margin to the top of the HBox you will put your Buttons into

HBox h = //...
HBox.setMargin(h, new Insets(20, 0, 0, 0));

Also is it possible to get text in two lines if the text size increases?

label.setWrapText(true);
1
On

The Button class does not support the .setSelected method, so we have to use the ToggleButton class:

    ToggleButton yesToggleButton = new ToggleButton("Yes");

    ToggleButton noToggleButton = new ToggleButton("No");

    noToggleButton.setSelected(true);

As you can also see, the word Toggle occurs in the ToggleButton class, which means that the button can either be selected or not.


Now we have to assign the objects of the class ToggleButton to a ToggleGroup object, because the objects of the class ToggleButton can be managed by the class ToggleGroup:

ToggleGroup toggleGroup = new ToggleGroup();

yesToggleButton.setToggleGroup(toggleGroup);

noToggleButton.setToggleGroup(toggleGroup);

To create a distance from the buttons, we must now specify the coordinates for the abscissa axis and for the ordinate axis:

    yesToggleButton.setTranslateX(-100); // abscissa axis
    yesToggleButton.setTranslateY(25);  // ordinate axis

    noToggleButton.setTranslateX(100); // abscissa axis
    noToggleButton.setTranslateY(25); // ordinate axis

If the font size increases and the font is no longer readable, it can be displayed on another line using the .setWrapText method:

messageType.setFont(Font.font(20)); // sets the font size

messageType.setWrapText(true);

You have used the VBox class, which aligns objects vertically. Since you want the objects to be horizontal, you can use HBox, or to align the objects in any direction, I would recommend StackPane:

StackPane root = new StackPane();

I would recommend this method to you to set the scene:

Stage confirmationWindow = new Stage();

confirmationWindow.setScene(new Scene(root, 540, 323));