Controlling the ESC key action in a JavaFX dialog

I have a JavaFX application which will initially show a Login dialog for user to key in user name and password. See below source codes.

If the user clicks on the "Connect" button, the application will perform the login with the entered user name and password, hides the Login dialog, and then shows the main window.

If the user clicks on the "Exit" button or the "X" close button, an alert will be shown to get the user's confirmation. If the user confirms, the application exits.

My problem is what happens when the user press the Escape key when the Login dialog is showing. When this key is pressed, the exit confirmation alert will be shown and immediately after that it will be closed. So what we see is the exit confirmation dialog showing up momentarily whenever the Escape key is pressed.

Why is this happening?

I want pressing the Escape key to be equivalent to clicking on the "Exit" or "X" button. That is, when Escape key is pressed, the exit confirmation dialog is shown.

Alternatively, is it possible to disable the Escape key altogether?

Thanks in advance.

public class TestApp extends Application {

    private Stage primaryStage;

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage stage) throws Exception {
        this.primaryStage = stage;
        HBox pane = new HBox();
        pane.setAlignment(Pos.CENTER);
        Scene scene = new Scene(pane, 300, 300);
        stage.setScene(scene);
        showLoginDialog();
    }

    public void showLoginDialog() {
        Dialog<String> loginDialog = new Dialog<>();
        loginDialog.setTitle("Login");
        loginDialog.setHeaderText("Enter User Name and Password to login.");
        loginDialog.setResizable(false);
        Label userNameLabel = new Label("User Name:");
        Label passwordLabel = new Label("Password:");
        TextField userNameField = new TextField();
        userNameField.setMaxWidth(Double.MAX_VALUE);
        PasswordField passwordField = new PasswordField();
        passwordField.setMaxWidth(Double.MAX_VALUE);
        GridPane grid = new GridPane();
        grid.setAlignment(Pos.CENTER);
        grid.setHgap(10);
        grid.setVgap(10);
        grid.setPadding(new Insets(20, 35, 20, 35));
        grid.add(userNameLabel, 1, 1);
        grid.add(userNameField, 2, 1);
        grid.add(passwordLabel, 1, 2);
        grid.add(passwordField, 2, 2);
        loginDialog.getDialogPane().setContent(grid);
        loginDialog.getDialogPane().getButtonTypes().add(ButtonType.OK);
        loginDialog.getDialogPane().getButtonTypes().add(ButtonType.CANCEL);
        Button connectButton = (Button) loginDialog.getDialogPane().lookupButton(ButtonType.OK);
        connectButton.setText("Connect");
        connectButton.addEventFilter(ActionEvent.ACTION, event -> {
            // perform login here
            loginDialog.hide();
            primaryStage.show();
            event.consume();
        });
        Button exitButton = (Button) loginDialog.getDialogPane().lookupButton(ButtonType.CANCEL);
        exitButton.setText("Exit");
        exitButton.addEventFilter(ActionEvent.ACTION, event -> {            
            handleExit();
            event.consume();
        });
        Stage stage = (Stage) loginDialog.getDialogPane().getScene().getWindow();
        stage.setOnCloseRequest(event -> {
            handleExit();
            event.consume();
        });
        stage.show();
    }

    private void handleExit() {
        Alert alert = new Alert(AlertType.CONFIRMATION, "", ButtonType.YES, ButtonType.NO);
        alert.setHeaderText("Confirm exit?");
        alert.resultProperty().addListener((observable, previous, current) -> {
            if (current == ButtonType.YES) {
                System.exit(1);
            }
        });
        alert.show();
    }

}

1 answer

  • answered 2017-10-11 10:22 Sedrick Jefferson

    See if this makes a difference:

    private void handleExit() {
        Alert alert = new Alert(AlertType.CONFIRMATION, "", ButtonType.YES, ButtonType.NO);
        alert.setHeaderText("Confirm exit?");
    
        Optional<ButtonType> result = alert.showAndWait();
        if(result.get() == ButtonType.YES)
        {
             Platform.exit();
        }
    
        alert.show();
    }