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);
}
}
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);
}
}
- duplicates
-
JDK-8131151 Pressing ESC in a nested alert closes self and parent alert
-
- Resolved
-