How to capture the closing event on a Stage?

3

I'm looking for an "windows close" Swing event.

    
asked by anonymous 09.11.2017 / 21:28

1 answer

2

You can call the setOnCloseRequest method of your < the Stage, so you can perform an action as soon as the user clicks on the button to close the window.

Ex:

public class Teste extends Application {
    @Override
    public void start(Stage stage) {
        stage.setOnCloseRequest(event -> System.out.println("Fechando o programa"));

        Parent root = FXMLLoader.load(getClass().getResource("main.fxml"));

        Scene scene = new Scene(root);

        stage.setScene(scene);
    }

    public static void main(String[] args) {
        launch(args);
    }
}
    
09.11.2017 / 21:52