0

I'm trying to learn javafx and I'm having some trouble with alert box interactions. My code is pretty straightforward:

public static void display(String title, String message) {
    Stage window = new Stage();

    window.initModality(Modality.APPLICATION_MODAL);
    window.setTitle(title);
    window.setMinWidth(250);

    Label label = new Label();
    label.setText(message);
    Button closeButton = new Button("Close the window");
    closeButton.setOnAction(e -> window.close());

    VBox layout = new VBox(10);
    layout.getChildren().addAll(label, closeButton);
    layout.setAlignment(Pos.CENTER);

    Scene scene = new Scene(layout);
    window.setScene(scene);
    window.showAndWait();
}

The problem is I can still somewhat interact with my initial window that I used to open the alert box, mainly clicking on the initial window brings it in front of the alert box. To my understanding, this shouldn't happen. Here is a gif demonstrating the issue: https://gyazo.com/0c2b69ec39f849227560fbdf2099c07c

Here is my code that calls the alert box

    public void start(Stage primaryStage) {
    window = primaryStage;
    window.setTitle("Alert Box test");

    button = new Button("Click me");
    button.setOnAction(e -> AlertBox.display("New Alert", "Don't forget to close this window!"));

    StackPane layout = new StackPane();
    layout.getChildren().add(button);
    Scene scene = new Scene(layout, 300, 250);
    window.setScene(scene);
    window.show();
}

What am I doing wrong here or is that just the intended behavior?

guster
  • 21
  • 2
  • JavaFX actually includes an `Alert` class that handles much of this for you. Check out [this tutorial](https://code.makery.ch/blog/javafx-dialogs-official/) to learn more. – Zephyr Jan 03 '19 at 02:11
  • The same behavior doesn't happen for me (Windows 10, OpenJFX 11.0.1). – Slaw Jan 03 '19 at 02:16
  • Take a look at https://stackoverflow.com/questions/10486731/how-to-create-a-modal-window-in-javafx-2-1 -- does setting the owner with `initOwner` help? – casablanca Jan 03 '19 at 03:49
  • What JavaFX version are you using? – user1803551 Jan 03 '19 at 09:54

0 Answers0