Alert Box For When User Attempts to close application using setOnCloseRequest in JavaFx

前端 未结 2 1664
难免孤独
难免孤独 2020-12-09 05:56

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 t

2条回答
  •  醉梦人生
    2020-12-09 06:37

    This answer is based on the answer to Javafx internal close request. That question is different from this question, but the answer is very similar.

    import javafx.application.Application;
    import javafx.event.EventHandler;
    import javafx.geometry.Insets;
    import javafx.scene.Scene;
    import javafx.scene.control.*;
    import javafx.scene.layout.StackPane;
    import javafx.stage.*;
    import javafx.stage.WindowEvent;
    
    import java.util.Optional;
    
    public class CloseConfirm extends Application {
    
        private Stage mainStage;
    
        @Override
        public void start(Stage stage) throws Exception {
            this.mainStage = stage;
            stage.setOnCloseRequest(confirmCloseEventHandler);
    
            Button closeButton = new Button("Close Application");
            closeButton.setOnAction(event ->
                    stage.fireEvent(
                            new WindowEvent(
                                    stage,
                                    WindowEvent.WINDOW_CLOSE_REQUEST
                            )
                    )
            );
    
            StackPane layout = new StackPane(closeButton);
            layout.setPadding(new Insets(10));
    
            stage.setScene(new Scene(layout));
            stage.show();
        }
    
        private EventHandler confirmCloseEventHandler = event -> {
            Alert closeConfirmation = new Alert(
                    Alert.AlertType.CONFIRMATION,
                    "Are you sure you want to exit?"
            );
            Button exitButton = (Button) closeConfirmation.getDialogPane().lookupButton(
                    ButtonType.OK
            );
            exitButton.setText("Exit");
            closeConfirmation.setHeaderText("Confirm Exit");
            closeConfirmation.initModality(Modality.APPLICATION_MODAL);
            closeConfirmation.initOwner(mainStage);
    
            // normally, you would just use the default alert positioning,
            // but for this simple sample the main stage is small,
            // so explicitly position the alert so that the main window can still be seen.
            closeConfirmation.setX(mainStage.getX());
            closeConfirmation.setY(mainStage.getY() + mainStage.getHeight());
    
            Optional closeResponse = closeConfirmation.showAndWait();
            if (!ButtonType.OK.equals(closeResponse.get())) {
                event.consume();
            }
        };
    
        public static void main(String[] args) {
            launch(args);
        }
    
    }
    

    The answer does use event.consume(), which you said you tried and did not work for you (though I'm not sure why not as it works fine for me with this sample code).

提交回复
热议问题