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

[Dialog] Intercepting the Cancel-Button-Action closes confirmation-Dialog automatically

    XMLWordPrintable

Details

    • Bug
    • Resolution: Duplicate
    • P4
    • 9
    • 8u51
    • javafx
    • None
    • x86_64
    • windows_7

    Description

      I want to intercept the Cancel-Button-Action of a Dialog to ask the user if he really want's to close the Dialog.
      For that reason I want to show a confirmation-Dialog. This works fine if I click the Cancel-Button, but doesn't work if Escape-Key is pressed. If Escape-Key is pressed the confirmation is shown for some milliseconds but hides immediately.

      It's the same with 8u60 Build b26.

      ================= EXAMPLE =======================

      import javafx.application.Application;
      import javafx.event.ActionEvent;
      import javafx.scene.Node;
      import javafx.scene.control.Alert;
      import javafx.scene.control.Alert.AlertType;
      import javafx.scene.control.ButtonBar.ButtonData;
      import javafx.scene.control.ButtonType;
      import javafx.stage.Stage;

      public class DialogCascadeEscapeProblemRunTest extends Application {

      @Override
      public void start(Stage primaryStage) throws Exception {
      Alert alert = new Alert(AlertType.CONFIRMATION);
      alert.setContentText("Do something critical...");
      // Intercept OK
      Node buttonOk = alert.getDialogPane().lookupButton(ButtonType.OK);
      buttonOk.addEventFilter(ActionEvent.ACTION, e2 -> {
      e2.consume();
      Alert okConfirmation = new Alert(AlertType.CONFIRMATION);
      okConfirmation.setContentText("Really execute ?");
      okConfirmation.showAndWait().ifPresent(c -> {
      if (c.getButtonData() == ButtonData.OK_DONE) {
      alert.close();
      }
      });
      });
      // Intercept Cancel
      Node buttonCancel = alert.getDialogPane().lookupButton(ButtonType.CANCEL);
      buttonCancel.addEventFilter(ActionEvent.ACTION, e2 -> {
      e2.consume();
      // This Alert will never appear if Escape Key is pressed because it's closed automatically
      Alert cancelConfirmation = new Alert(AlertType.CONFIRMATION);
      cancelConfirmation.setContentText("Really cancel ?");
      cancelConfirmation.showAndWait().ifPresent(c -> {
      if (c.getButtonData() == ButtonData.OK_DONE) {
      alert.close();
      }
      });
      });
      alert.show();
      }

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

      }

      Attachments

        Issue Links

          Activity

            People

              jgiles Jonathan Giles
              aliebelt Andreas Liebelt
              Votes:
              0 Vote for this issue
              Watchers:
              1 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: