JavaFX 2.0: Closing a stage (window)

javafx close stage from controller
javafx close window event in controller
javafx close window and open another
javafx close application
how to close window in java fx
javafx open new window and close old
javafx intercept window close
javafx cancel window close

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!

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.

javafx.stage.Stage.close java code examples, 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  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.

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

((Node)(event.getSource())).getScene().getWindow().hide();

JavaFX 3, @FXML private javafx.scene.control.Button closeButton; @FXML private void closeButtonAction(){ // get a handle to the stage Stage stage = (Stage) closeButton. showingProperty() Whether or not this Stage is showing (that is, open on the user's system). sizeToScene() Set the width and height of this Window to match the size of the content of this Window's Scene. widthProperty() The width of this Stage. xProperty() The horizontal location of this Stage on the screen.

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

JavaFX Java GUI Tutorial - 7, You might find these following videos helpful :-) Introduction to Java playlist: https​://goo.gl Duration: 6:22 Posted: May 19, 2017 Close stage window. import javafx.application.Application; import javafx.scene.Scene; import javafx.scene.layout.VBox; import javafx.scene.text.Font; import javafx

Stage (JavaFX 2.2), 35 videos Play all JavaFX Java GUI Design Tutorialsthenewboston · JavaFX Java GUI Duration: 8:22 Posted: Mar 22, 2015 WINDOW_CLOSE_REQUEST public static final EventType<WindowEvent> WINDOW_CLOSE_REQUEST This event is delivered to a window when there is an external request to close that window. If the event is not consumed by any installed window event handler, the default handler for this event closes the corresponding window.

Stage (JavaFX 8), When a parent window is closed, all its descendant windows are closed. The same chained behavior applied for a parent window that is iconified. A stage will​  this stage has a non-null owner window, and its owner is closed the user closes the window via the window system (for example, by pressing the close button in the window decoration) After the Stage is hidden, and the application has returned from the event handler to the event loop, the nested event loop terminates and this method returns to the caller.

Java Code Examples javafx.stage.Window, The JavaFX Stage class is the top level JavaFX container. When a parent window is closed, all its descendant windows are closed. Since: JavaFX 2.0  Window size vs Scene size The javafx.scene.Scene class is responsible for rendering JavaFX 2.0 content. A scene can be placed into various containers, for example, javafx.stage.Stage or javafx.embed.swing.JFXPanel. The size of a container may be or may not be equal to the size of the scene attached to it.

Comments
  • stackoverflow.com/questions/13567019/…
  • Thanks for explaining, it really helped my understanding! :)
  • 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.
  • I don't understand your comment stefan. You might want to create a new question instead.
  • 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.Node