Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8210214

Dialog/Alert: must be showing in onShown handler

XMLWordPrintable

      To reproduce, run the example below and click the button to open an alert - it's implemented to have a onShown handler which checks the value of the alert's showingProperty

      - expected: true
      - actual: false

      Happens only if opening the dialog with showAndWait.

      public class DialogOnShown extends Application {

          @Override
          public void start(Stage primaryStage) {
              Button btn = new Button();
              btn.setText("Say 'Hello World'");
              btn.setOnAction(e -> {
                  Alert alert = new Alert(Alert.AlertType.INFORMATION);
                  alert.setHeaderText("This is an alert!");
                  alert.setContentText("hello again!");
                  alert.initOwner(primaryStage);
                  alert.setOnShown(shown -> {
                      System.out.println("alert must be showing:" + alert.isShowing());
                  });
                  alert.showAndWait();
              });

              StackPane root = new StackPane();
              root.getChildren().add(btn);

              Scene scene = new Scene(root, 300, 250);

              primaryStage.setTitle("Hello World!");
              primaryStage.setScene(scene);
              primaryStage.show();
          }

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

      }

      The issue bubbled up at SO https://stackoverflow.com/q/52062291/203657 in a use-case to size the dialog after it has been shown. A hack around is to listen to the showing property directly and do stuff as needed. On the other hand: that's exactly what the event handler hook is intended for. There must be a way to fire the event at the correct time, even though the dialog blocks.

            Unassigned Unassigned
            fastegal Jeanette Winzenburg
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated: