In Swing you can simply use setDefaultCloseOperation()
to shut down the entire application when the window is closed.
However in JavaFX I can't find an equivalent. I have multiple windows open and I want to close the entire application if a window is closed. What is the way to do that in JavaFX?
Edit:
I understand that I can override setOnCloseRequest()
to perform some operation on window close. The question is what operation should be performed to terminate the entire application?
stage.setOnCloseRequest(new EventHandler<WindowEvent>() {
@Override
public void handle(WindowEvent event) {
stop();
}
});
The stop()
method defined in Application
class does nothing.
Did you try this..setOnCloseRequest
There is one example
For reference, here is a minimal implementation using Java 8 :
This seemed to work for me:
The application automatically stops when the last
Stage
is closed. At this moment, thestop()
method of yourApplication
class is called, so you don't need an equivalent tosetDefaultCloseOperation()
If you want to stop the application before that, you can call
Platform.exit()
, for example in youronCloseRequest
call.You can have all these information on the javadoc page of
Application
: http://docs.oracle.com/javafx/2/api/javafx/application/Application.htmlSome of the provided answers did not work for me (javaw.exe still running after closing the window) or, eclipse showed an exception after the application was closed.
On the other hand, this works perfectly: