I am trying to prompt the user to confirm they want to close a program before exiting. In the event a task is still being executed, I wanted to confirm that the still wish to exit or give them a chance to allow the task to finish before exiting. I used setOnCloseRequest, but it did not work. Ive used event.consume which just seemed to disable the [x] button. Any suggestions are appreciated. I found a similar question here that did not work for me -> JavaFX stage.setOnCloseRequest without function?
Public class Sample extends Application {
@Override
public void start(Stage stage) throws Exception {
stage.setOnCloseRequest(event -> {
NewScanView checkScan = new NewScanView();
boolean isScanning = checkScan.isScanning();
Alert alert = new Alert(AlertType.CONFIRMATION);
alert.setTitle("Close Confirmation");
alert.setHeaderText("Cancel Creation");
alert.setContentText("Are you sure you want to cancel creation?");
if (isWorking == true){
Optional<ButtonType> result = alert.showAndWait();
if (result.get() == ButtonType.OK){
stage.close();
}
if(result.get()==ButtonType.CANCEL){
alert.close();
}
}
});
stage.setHeight(600);
stage.setWidth(800);
stage.setTitle("Title");
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}