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:
Now, I need to make two changes,
- 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.
- 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.
Put the buttons into a HBox
See here for a similar question
Add some margin to the top of the HBox you will put your Buttons into