Reputation: 95
I need show alert confirmation before application closes (X pressed Button)
public void start(Stage primaryStage) throws Exception {
this.primaryStage = primaryStage;
...
primaryStage.setOnHiding(event -> {
System.out.println("hidding");
Alert alert = new Alert(Alert.AlertType.CONFIRMATION, Lang.getString("exit_confirmation"));
Toolkit.getDefaultToolkit().beep();
Optional<ButtonType> result = alert.showAndWait();
if (result.isPresent() && result.get() != ButtonType.OK) {
return;
//don't close stage
}
});
}
But anyway primaryStage
is closed. How can i code in order to exit application only if ButtonType.OK
is pressed? Sorry for my english
Upvotes: 3
Views: 2474
Reputation: 674
It's always better to google your problem first as you can find a better explanation, but here's your answer
primaryStage.setOnCloseRequest(new EventHandler<WindowEvent>() {
@Override
public void handle(WindowEvent event) {
//your code goes here
//this line cancel the close request
event.consume();
}
});
Upvotes: 9