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();
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With