import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.ButtonType;
import javafx.scene.control.Label;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;

import java.util.Optional;

public class SampleAppTestCtrlQ extends Application {

   public static void main(String[] args) {
       launch(args);
   }

   @Override
   public void start(Stage stage) throws Exception {

       BorderPane tilePane = new BorderPane();
       tilePane.setCenter( new Label("Close this with Cmd-Q"));

       stage.setScene(new Scene(tilePane));
       stage.setOnCloseRequest(ev -> {
           Alert alert = new Alert(Alert.AlertType.CONFIRMATION);
           alert.setTitle("Confirmation Dialog");
           alert.setHeaderText("Save your work?");
           alert.setContentText("Press yes to save");

           Optional<ButtonType> result = alert.showAndWait();
           if (result.get() == ButtonType.OK){
               // do save
			   System.out.println("OK");
           } else {
               // ... user chose CANCEL or closed the dialog
			   System.out.println("Cancel");
           }
       });
       stage.show();
   }

}
