/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package oncloserequesttest; import javafx.application.Application; import javafx.event.ActionEvent; import javafx.event.EventHandler; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.layout.FlowPane; import javafx.stage.Stage; import javafx.stage.WindowEvent; /** * * @author rodrigo */ public class OnCloseRequestTest extends Application { /* If this is set to false, the application quits. Else, the Stage disappears (on Linux) but the application keeps running. */ private boolean consumeEvent = true; @Override public void start(final Stage primaryStage) { Button btn = new Button(); btn.setText("Say 'Hello World'"); btn.setOnAction(new EventHandler() { @Override public void handle(ActionEvent event) { System.out.println("Hello World!"); } }); Button exit = new Button(); exit.setText("exit"); exit.setOnAction(new EventHandler() { @Override public void handle(ActionEvent event) { consumeEvent = false; primaryStage.close(); } }); FlowPane root = new FlowPane(); root.getChildren().add(btn); root.getChildren().add(exit); Scene scene = new Scene(root, 300, 250); primaryStage.setTitle("Hello World!"); primaryStage.setScene(scene); primaryStage.setOnCloseRequest(new EventHandler () { @Override public void handle(WindowEvent t) { if (consumeEvent){ t.consume(); } System.out.println("Event is: "+(t.isConsumed()?"consumed":"NOT consumed")); System.out.println("Stage is: "+(primaryStage.isShowing()?"showing":"NOT showing")+" before call to show()"); primaryStage.show(); // Try to force the stage to show System.out.println("Stage is: "+(primaryStage.isShowing()?"showing":"NOT showing")+" after call to show()"); } }); primaryStage.show(); } /** * The main() method is ignored in correctly deployed JavaFX application. * main() serves only as fallback in case the application can not be * launched through deployment artifacts, e.g., in IDEs with limited FX * support. NetBeans ignores main(). * * @param args the command line arguments */ public static void main(String[] args) { launch(args); } }