import javafx.application.Application;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.stage.Modality;
import javafx.stage.Stage;
import javafx.stage.StageStyle;

public class CrashMe extends Application {

    @Override
    public void start(Stage stage) {
        ////////////////////////////////////////
        // create main screen
        ////////////////////////////////////////
        Button launchButton = new Button("Launch dialog");
        VBox vbox = new VBox(launchButton);
        Scene scene = new Scene(new StackPane(vbox), 640, 480);
        stage.setScene(scene);

        ////////////////////////////////////////
        // dialog which crashes the application
        ////////////////////////////////////////
        Button clickMe = new Button("Close dialog");
        BorderPane pane = new BorderPane(clickMe);
        Dialog dialog = new Dialog(stage, pane);
        Runnable dialogApply = () -> {
            System.out.println("Apply processed");
            dialog.close();
        };
        dialog.setApply(dialogApply);
        clickMe.setOnAction(e -> dialogApply.run());

        ////////////////////////////////////////
        // wire button on main screen to launch the dialog
        ////////////////////////////////////////
        launchButton.setOnAction(e -> {
            dialog.showAndWait();
        });
        stage.show();
    }

    public static void main(String[] args) {
        launch();
    }

    public static class Dialog extends Stage {
        private Runnable apply;

        public Dialog(Stage owner, Parent layout) {
            super(StageStyle.DECORATED);
            Scene layoutScene = new Scene(layout, 500, 500);
            this.setScene(layoutScene);

            layoutScene.addEventHandler(KeyEvent.KEY_PRESSED, event -> {
                if (event.getCode() == KeyCode.ENTER && event.isShortcutDown()) {
                    // this crashes the app on MacOs
                    System.out.println("META + ENTER");
                    if (apply != null) {
                        apply.run();
                    }
                    event.consume();
                    return;
                }

                if (event.getCode() == KeyCode.ENTER && event.isAltDown()) {
                    // this does not crash the app on MacOs
                    System.out.println("ALT + ENTER");
                    if (apply != null) {
                        apply.run();
                    }
                    event.consume();
                    return;
                }
            });

            this.hide();
            this.initModality(Modality.APPLICATION_MODAL);
            this.initOwner(owner);
            this.setResizable(true);
        }

        public void setApply(Runnable apply) {
            this.apply = apply;
        }

        @Override
        public void close() {
            super.close();
        }
    }
}