JavaFX 2.0: Closing a stage (window)
Asked Answered
R

3

37

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!

Rhinestone answered 18/4, 2012 at 15:11 Comment(1)
#13567519Building
F
55

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).

Formalin answered 18/4, 2012 at 20:6 Comment(5)
Thanks for explaining, it really helped my understanding! :)Rhinestone
I am wondering if Window.hide() is also equivalent to Stage.hide() and therefore Stage.close() if a window is the only window of the stage? So I wouldn't need to pass a reference to the stage.Calceiform
I don't understand your comment stefan. You might want to create a new question instead.Formalin
Then it comes to the question, How can we hide the stage without closing it completely?Misfeasor
@TharinduSathischandra Updated answer based upon your question.Formalin
L
24

This worked perfectly for me (with the import for Node):

((Node)(event.getSource())).getScene().getWindow().hide();
Leg answered 21/2, 2014 at 15:38 Comment(1)
I'm trying to hide window from menuitem ActionEvent but getting exception "menuitem cannot be casted to Node". Here is exception Caused by: java.lang.ClassCastException: javafx.scene.control.MenuItem cannot be cast to javafx.scene.NodeSpiderwort
S
3

For the users also interested in listening to the close window event, add an event filter to the window: (this event is also fired when the user press the OS close button of the application)

    yourWindow.addEventFilter(WindowEvent.WINDOW_CLOSE_REQUEST, event -> {
        // add your code here to handle the close event
        // use event.consume(); to prevent the application from closing
    });

If you need to close the application with a custom close button, in the onAction method of the button fire the event :

 yourWindow.fireEvent(new WindowEvent(window, WindowEvent.WINDOW_CLOSE_REQUEST));
Stanzel answered 8/9, 2018 at 12:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.