I'm making a application in JavaFX 2.0. From my main window I am starting a new window with some settings. After I am done adjusting the settings I want to press a button like "Save changes".
I would like this button to save the changes and close the window. By closing i mean killing it, not placing it in the background or setting the visibility. I've read about a method Stage.close()
http://docs.oracle.com/javafx/2.0/api/javafx/stage/Stage.html
As you can see it's similar to the method Hide(), which only hides the window, not closing it.
Q: Anybody knows any methods or have some code that would help me close a window?
All help will be greatly appreciated. Thanks!
Be default, to quit the program is to let the user click the standard window close button, typically represented by an X in the upper-right corner of the window's title bar.
JavaFX Stage is a standalone application displaying window. Stage is primary element in JavaFX. We can add as many stage as we want but we have only one primary stage.
A Stage in JavaFX is a top-level container that hosts a Scene, which consists of visual elements. The Stage class in the javafx. stage package represents a stage in a JavaFX application. The primary stage is created by the platform and passed to the start(Stage s) method of the Application class.
Stage , represents a window in a JavaFX desktop application. Inside a JavaFX Stage you can insert a JavaFX Scene which represents the content displayed inside a window - inside a Stage .
The documentation you linked states that stage.close()
:
Closes this Stage. This call is equivalent to hide().
As hide()
is equivalent to close()
and close()
closes the stage, then hide()
also closes the stage.
When all stages in an application are hidden (or closed if you like, because it is the same thing), the application exits. Confusing, I know, but that's just the way the JavaFX team decided to name and implement the actions.
If desired, the Platform.setImplicitExit(boolean) method can be used to switch off the default behaviour of exiting the application when the last window is closed or hidden.
Then it comes to the question, How can we hide the stage without closing it completely?
I don't think hide()
or the equivalent close()
method will close the stage "completely" as in freeing up all resources related to the window (as long as you keep a reference to the stage around somewhere). I think it just makes it so that the stage is not visible. You could probably call show()
after calling close()
and the window would likely be made visible again (I didn't try it). Though, if you were to do that, then it would be more intuitive to call hide()
rather than close()
.
My guess is that if you no longer keep any references to a stage in your application and the stage is closed or hidden, then perhaps the JVM will release all resources related to the stage whenever its algorithm decides to garbage collect those resources (again I didn't test this and it may not work that way).
This worked perfectly for me (with the import for Node
):
((Node)(event.getSource())).getScene().getWindow().hide();
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