GeelZyen
GeelZyen

Reputation: 11

How to call default close operation of a stage in javafx?

I wrote the defaultCloseOperation function of the primaryStage, but I have an exit button too and I want to run that defaultCloseOperation. I tried to call the close() and the hide() methods of the stage but it exit immediately without calling my defaultCloseOperation function, but I need to call it because I need to release all the resources from the server side when I close the client.

Upvotes: 0

Views: 1868

Answers (2)

Vasyl Lyashkevych
Vasyl Lyashkevych

Reputation: 2222

you can see it:

stage.setOnCloseRequest(new EventHandler<WindowEvent>() {
    @Override public void handle(WindowEvent t) {
        System.out.println("CLOSING");
    }
});

and here:

new EventHandler<ActionEvent>() {
  @Override public void handle(ActionEvent actionEvent) {
    // take some action
    ...
    // close the dialog.
    Node  source = (Node)  actionEvent.getSource(); 
    Stage stage  = (Stage) source.getScene().getWindow();
    stage.close();
  }
}

more of explanation you can read here

Upvotes: 1

fabian
fabian

Reputation: 82461

Do not do this on a closing operation of a Stage.

This is what the Application.stop method should be used for.

@Override
public void stop() throws Exception {
    // TODO: release resources here
}

If there are resources used for one of multiple windows however, you should use an event handler for the onHidden event - no need to extend Stage:

stage.setOnHidden(event -> {
    // TODO: release resources here
});

Upvotes: 4

Related Questions