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?
Asked
Active
Viewed 9,632 times
9
-
There's no real concept of "disposing" a stage in JavaFX. If a stage is hidden and the reference goes out of scope, its resources will be cleaned up. If you keep a reference in scope, then you can call `show()` on it at a later time to display it again. – James_D Sep 02 '15 at 14:05
-
The problem is, i have a daemon thread that runs on background and takes some time to finish. And when it finish, it should open the stage again. The problem is that when the user closes the window, the application stops. – Mateus Viccari Sep 02 '15 at 14:17
-
2You coul'd minimize you application to the system tray. Here is an example http://javafx-demos.googlecode.com/svn-history/r100/trunk/javafx-demos/src/main/java/com/ezest/javafx/demogallery/swingawtintegration/JavaFXOnTrayIconDemo.java – Rafael Guillen Sep 02 '15 at 14:24
-
1If your thread operates that way then it shouldn't be daemon (Pretty much the definition of a non-daemon thread is that it should keep the system from exiting until it's done). – Bill K Oct 13 '16 at 21:56
-
You're right, i was trying to solve a problem the wrong way back when i asked the question. – Mateus Viccari Oct 14 '16 at 13:54
1 Answers
30
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).

James_D
- 201,275
- 16
- 291
- 322
-
Sorry if this is a bit dated, but out curiosity, would you happen to know how to show a program that has been hidden? – ZenOokami Jul 28 '16 at 17:12
-
Assuming you mean show a window that has been hidden, I would imagine that calling [`show()`](http://docs.oracle.com/javase/8/javafx/api/javafx/stage/Stage.html#show--) would do what you want. – James_D Jul 28 '16 at 17:30
-
1