Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

On JavaFX, how to hide stage without disposing it and closing the application?

Tags:

java

javafx

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?

like image 599
Mateus Viccari Avatar asked Sep 02 '15 14:09

Mateus Viccari


People also ask

How do you close a JavaFX 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.

Can you have multiple stages displayed in JavaFX?

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.

Can a JavaFX application have more than one stage object?

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.

What method is invoked to display a stage 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.


1 Answers

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

like image 143
James_D Avatar answered Nov 16 '22 04:11

James_D