Ramya Pratyusha
Ramya Pratyusha

Reputation: 11

How to detect if my javaFX application has been closed?

I am developing a javafx application having multiple scenes which get executed whenever a button in some previous scene gets pressed. I want to get some action done whenever the application gets closed at any stage. How to achieve this? Help me with this background task to be achieved.

Upvotes: 1

Views: 518

Answers (1)

Use a lambda expression where you defined the stage. Example, if you're using a Stage is named primaryStage

public class Main extends Application {
    @Override
    public void start(Stage primaryStage) throws Exception {
        Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
        primaryStage.setTitle("Hello World");
        primaryStage.setScene(new Scene(root, 300, 275));
        primaryStage.setOnCloseRequest(event -> {
            // --- whatever task you want to execute on close goes here ---
        });
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}

Upvotes: 1

Related Questions