Javafx close all windows

JavaFX 2.0: Closing a stage (window)

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

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!

3 Answers 3

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

JavaFx Controller: detect when stage is closing

I have a javaFx project and I would like to implement a method inside the Controller that is invoked every time the stage(window) is closing this to be able to perform some actions before closing it. The desire to have the method placed inside the controller is because the actions to be taken depend on the choices that the user has made while using the scene.

Читайте также:  Как загружать windows через bios

My goal is every time the user closes the stage, to have a print and then an user related action.

I try inside the controller class:

but it’s no effective.

1 Answer 1

Just from a design perspective, it doesn’t really make sense in the controller to associate a handler with the stage, as the stage is typically not part of the view to which the controller is connected.

A more natural approach is to define a method in the controller, and then invoke that method from a handler which you associate with the stage at the point where the stage is created.

So you would do something like this in the controller:

and then at the point where you load the FXML you would do

Again, exiting the application is probably not (or probably should not be) the responsibility of the controller (it’s the responsibility of the class that creates the window or manages application life-cycle), so if you really needed to force an exit when the window closed, you would probably move that to the onHidden handler:

JavaFX application still running after close

I’m having problem to close my javaFX application, when I click the close button from my stage, my application disappears but if I look for it in my task manager my application still there without close. I’ve tried to use this code below to force it close the main thread and all childrens threads but the problem persists.

8 Answers 8

Does your application spawn any child threads? If so have you ensured that you terminate them (assuming that they’re not daemon threads)?

If your application spawns non-daemon threads then they (and therefore your app) will continue to live on until such time you kill the process

The only way was to call System.exit(0);

[EDITED]

System.exit will just hide your application, if you open SO’s manager task your application will be there. The correct way is to check your Threads, one by one and close all before close application.

I was able to fix this problem by calling com.sun.javafx.application.tkExit() . You can read more in my other answer here: https://stackoverflow.com/a/22997736/1768232 (these two questions really are duplicates).

I currently had this problem while using an ThreadExecutor in the controller. Application does not exit if the ThreadExecutor is not shutdown. See here: how-to-shut-down-all-executors-when-quitting-an-application

As it can be a problem to recognize an application exit in the controller, you can get a reference to the controller from your Application class like so (using the sample application from Eclipse):

Читайте также:  Windows не разрешает пинги

Your Application overrides the stop method, where you can call a housekeeping method of the controller (i use a method called startHousekeeping):

Just a note: Try checking if you use

Had a similar problem and overflowing my tasks. The above line will not make the stage close, it will hide it.

To imitate pressing ‘x’ one can do:

You could close your application by clicking close Button , with some code.

Not the answer you’re looking for? Browse other questions tagged java javafx-2 javafx or ask your own question.

Linked

Hot Network Questions

Subscribe to RSS

To subscribe to this RSS feed, copy and paste this URL into your RSS reader.

site design / logo © 2021 Stack Exchange Inc; user contributions licensed under cc by-sa. rev 2021.4.16.39093

By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.

javafx.scene.control.Dialog won’t close on pressing “x”

If I just create an empty class extending from javafx.scene.control.Dialog , it won’t close when I’m pressing the «x» button in the top right corner.

How do I implement this behaviour? The API seems to tell me that I need to implement a close button. But in my case I don’t want a close button, I just want to close the window with the x button or by pressing ESC. Is this possible?

5 Answers 5

To work-around this, you could add a hidden close button to the dialog.

Then the dialog meets both your requirement of being able to be closed via the native windowing system’s window close icon as well as the JavaFX Dialog requirement of including a close button in the dialog for the close icon to work.

Alternately, you could use a Stage with showAndWait instead of a Dialog. A Stage without any included buttons is closable using the windowing system’s close window icon.

The workaround from @eckig or @jewelsea works pretty fine. But I would use something like this:

I do not know any constrains of this use, but it worked for me. And I recommend initialize window right after dialog initialization, like above.

JavaFX dialogs can only be closed ‘abnormally’ (as defined above) in two situations:

When the dialog only has one button, or

When the dialog has multiple buttons, as long as one of them meets one of the following requirements:

Alerts in Javafx do not close when “x” button is pressed

Hi for a different javafx application I’ve been testing alerts and the only thing which doesn’t work in pressing the «X» button for the alert box.

I have added a code below but if you don’t have time to run it here is a GIF of explaining what issue I have with the alertbox: https://giant.gfycat.com/GeneralUntimelyBluewhale.webm

I am not quite sure how to upload gifs to the actual post so sorry for that.

Читайте также:  После обновления windows 10 поменялись цвета

Is there any way of fixing this issue?

2 Answers 2

As per the «Dialog Closing Rules» in the Dialog API documentation, the default «X» button works normally only if atleast one of the buttons is of type «CANCEL». So changing any one of your button to ButtonType.CANCEL should close your dialog on click of «X».

If you are not interested in using built in buttons, then you have to explicitly handle the close request of dialog as per your requirement.

To add to Sai Dandem’s answer, here is the relevant javadoc from Dialog :

Dialog Closing Rules

It is important to understand what happens when a Dialog is closed, and also how a Dialog can be closed, especially in abnormal closing situations (such as when the ‘X’ button is clicked in a dialogs title bar, or when operating system specific keyboard shortcuts (such as alt-F4 on Windows) are entered). Fortunately, the outcome is well-defined in these situations, and can be best summarised in the following bullet points:

  • JavaFX dialogs can only be closed ‘abnormally’ (as defined above) in two situations:
    1. When the dialog only has one button, or
    2. When the dialog has multiple buttons, as long as one of them meets one of the following requirements:
      1. The button has a ButtonType whose ButtonBar.ButtonData is of type ButtonBar.ButtonData.CANCEL_CLOSE.
      2. The button has a ButtonType whose ButtonBar.ButtonData returns true when ButtonBar.ButtonData.isCancelButton() is called.
  • In all other situations, the dialog will refuse to respond to all close requests, remaining open until the user clicks on one of the available buttons in the DialogPane area of the dialog.
  • If a dialog is closed abnormally, and if the dialog contains a button which meets one of the two criteria above, the dialog will attempt to set the result property to whatever value is returned from calling the result converter with the first matching ButtonType.
  • If for any reason the result converter returns null, or if the dialog is closed when only one non-cancel button is present, the result property will be null, and the showAndWait() method will return Optional.empty(). This later point means that, if you use either of option 2 or option 3 (as presented earlier in this class documentation), the Optional.ifPresent(java.util.function.Consumer) lambda will never be called, and code will continue executing as if the dialog had not returned any value at all.

Normally, when using AlertType.CONFIRMATION , there would already be a cancel button. However, you’re declaring your own buttons in the constructor of your Alert which overrides the default buttons.

By passing in a variable number of ButtonType arguments, the developer is directly overriding the default buttons that will be displayed in the dialog, replacing the pre-defined buttons with whatever is specified in the varargs array.

Оцените статью