Anarkie Anarkie - 1 year ago 254
Java Question

How to refresh parent window after closing child window in JavaFX?

I have a parent Window which displays people i.e:




I click the Edit button, child window opens, change first name and then click accept, child window closes, the changes are in mysql database saved but the parent window still shows the old values. I do have a refresh method which works, but how can I call it from the child window or set it to be called after closing the child window?

Both Parent and Child Windows have controller and fxml. Any help would be appreciated.

Answer Source

James_D's Answer (better)

Thanks to @James_D, you should use setOnHidden instead of setOnCloseRequest, that way you can again just call .close() and the EventHandler will be called.

My Answer

You could make use of setOnCloseRequest:

childStage.setOnCloseRequest(new EventHandler<WindowEvent>() {
    public void handle(WindowEvent we) {
        // Refresh the parent window here

To invoke this close request on a child form you can do:

    childStage.getOnCloseRequest().handle(new WindowEvent(childStage, WindowEvent.WINDOW_CLOSE_REQUEST));
    // or
    childStage.fireEvent(new WindowEvent(childStage, WindowEvent.WINDOW_CLOSE_REQUEST));

So you could implement the close button on the child stage like so:

Button closeButton = new Button("Close");
closeButton.setOnAction(event -> childStage.fireEvent(new WindowEvent(childStage, WindowEvent.WINDOW_CLOSE_REQUEST)));
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download