I have the following workflow in my application that is causing a problem:
Click Button to Open Dialog > Open Dialog > Click Button From Dialog > Display Confirmation Alert > Upon Confirmation close the first dialog and open a new dialog
The second dialog does not allow input into the TextField. I have included a SSCE that displays the problem. One additional weird thing is that if you try to close the second dialog by clicking the 'X', and then close the Alert then I am able to type into the field.
public class DialogTest extends Application {
@Override
public void start(Stage primaryStage) {
Button button = new Button("Show Dialog");
VBox root = new VBox(10, button);
root.setAlignment(Pos.CENTER);
Scene scene = new Scene(root, 350, 120);
primaryStage.setScene(scene);
primaryStage.show();
button.setOnAction(event -> {
Dialog<Pair<String, String>> dialog = getDialog(scene.getWindow(), "Dialog 1", true);
dialog.showAndWait();
});
}
public static void main(String[] args) {
launch(args);
}
public Dialog<Pair<String, String>> getDialog(Window owner, String title, boolean addButton) {
Dialog<Pair<String, String>> dialog = new Dialog<>();
dialog.setTitle(title);
dialog.initOwner(owner);
dialog.getDialogPane().getButtonTypes().addAll(ButtonType.OK, ButtonType.CANCEL);
if(addButton) {
Button button = new Button("Show Dialog");
dialog.getDialogPane().setContent(button);
button.setOnAction(event -> {
Alert alert = new Alert(AlertType.CONFIRMATION, "Are you sure?", ButtonType.YES, ButtonType.NO);
alert.initOwner(owner);
if(alert.showAndWait().get() == ButtonType.YES) {
dialog.close();
Dialog<Pair<String, String>> dialog2 = getDialog(owner, "Dialog 2", false);
TextField textField = new TextField();
dialog2.getDialogPane().setContent(textField);
dialog2.getDialogPane().getScene().getWindow().setOnCloseRequest(closeEvent -> {
closeEvent.consume();
if(textField.getText().trim().isEmpty()) {
Alert alert2 = new Alert(AlertType.ERROR, "Please enter a value", ButtonType.OK);
alert2.initOwner(dialog2.getDialogPane().getScene().getWindow());
alert2.showAndWait();
}
});
dialog2.showAndWait();
}
});
}
return dialog;
}
}