javafx Alert


Alert is a simple popup that displays a set of buttons and gets an result depending on the button the user clicked:


This lets the user decide, if (s)he really wants to close the primary stage:

public void start(Stage primaryStage) {
    Scene scene = new Scene(new Group(), 100, 100);

    primaryStage.setOnCloseRequest(evt -> {
        // allow user to decide between yes and no
        Alert alert = new Alert(Alert.AlertType.CONFIRMATION, "Do you really want to close this application?", ButtonType.YES, ButtonType.NO);

        // clicking X also means no
        ButtonType result = alert.showAndWait().orElse(ButtonType.NO);
        if (ButtonType.NO.equals(result)) {
            // consume event i.e. ignore close request 

Note that the button text is automatically adjusted depending on the Locale.