How to capture the closing event in a Stage?

Asked

Viewed 270 times

3

I’m looking for an event like "windows close" Swing.

1 answer

2


You can call the method setOnCloseRequest of your Stage, this way you can perform an action as soon as the user clicks 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);
    }
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.