import javafx.application.Application; import javafx.application.Platform; import javafx.event.ActionEvent; import javafx.event.EventHandler; import javafx.scene.Group; import javafx.scene.Node; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.control.Label; import javafx.scene.input.KeyCode; import javafx.scene.input.KeyEvent; import javafx.scene.text.Text; import javafx.stage.Stage; public class EscCrash extends Application { Stage stage = null; public static void main(String[] args) { launch(args); } public void makeEscapeClosable(final Scene scene) { scene.setOnKeyPressed(new EventHandler() { public void handle(KeyEvent ke) { if (ke.getCode() == KeyCode.ESCAPE) { stage.close(); } } }); } @Override public void start(Stage primaryStage) throws Exception { stage = primaryStage; Button btn = new Button("Button"); btn.setOnAction(new EventHandler() { @Override public void handle(ActionEvent event) { Label label = new Label("Label"); Stage st = new Stage(); st.initOwner(stage); st.setScene(new Scene(label)); makeEscapeClosable(st.getScene()); st.showAndWait(); } }); stage.setScene(new Scene(new Group(btn))); makeEscapeClosable(stage.getScene()); stage.show(); stage.setX(stage.getX() - 100); // Platform.setImplicitExit(false); } }