Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to clean up the program when exiting javafx program?

I want my JavaFX program to clean up the program when I press the x button in the top right corner of the window. How can I do that? The cleanup code is

        JIntellitype.getInstance().cleanUp();
like image 635
Karuvägistaja Avatar asked Feb 02 '23 07:02

Karuvägistaja


1 Answers

If you have multiple windows and the cleanup is just for resources allocated to the window being closed, add an appropriate eventhandler for the window's setOnHidden event.

A description of the Window.setOnHidden event:

Called just after the Window has been hidden. When the Window is hidden, this event handler is invoked allowing the developer to clean up resources or perform other tasks when the Window is closed.

If you create multiple Stages for your JavaFX Application, the Application will close when the last Stage for the Application closes. When the Application closes, then the Application's stop method is called, which you can implement by overriding it in your Application class.

A description of the Application.stop method:

This method is called when the application should stop, and provides a convenient place to prepare for application exit and destroy resources.

In your case, providing a implementation of Application.stop is probably the most appropriate solution.

like image 110
jewelsea Avatar answered Feb 04 '23 22:02

jewelsea