I need to creat an app that shows the primary stage to the user, and if the user closes the stage, instead of finishing the application, it should just hide the stage for later use. On swing we could just call setVisible(false) to a JFrame or JDialog, but how to do it on JavaFX?
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.
In JavaFX, an application can only have one stage but that stage can have 1 or several scenes. Therefore, we can create multiple scenes for a given JavaFX application. In this program, we will create 2 scenes and show how to switch between the scenes in our code. So the Java code is shown below.
This primary stage object creates a window to display user created functionality in the output. If your requirement is more than one stage then we can create as many Stage objects as we want. JavaFX Stage class available in javafx.
Showing a Stage The difference between the JavaFX Stage methods show() and showAndWait() is, that show() makes the Stage visible and the exits the show() method immediately, whereas the showAndWait() shows the Stage object and then blocks (stays inside the showAndWait() method) until the Stage is closed.
Once the JavaFX toolkit has started, by default it will close down when the last visible window is closed.
To prevent this, you can call
Platform.setImplicitExit(false);
You typically do this in your start(...)
method, though it can be called from any thread.
To exit the application, you would then need to call
Platform.exit();
(as the application no longer exits automatically).
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