Davidaj
Davidaj

Reputation: 235

actionEvent on click, want the same for enter key java fx

I'm new to javafx and i'm using an action property for when the login button is clicked by the mouse, I wish to use the same code for when the enter key is pressed, is there a more efficient way to do this other than copying the same code. below is the code in my login controller for javafx:

@FXML
    private void handleButtonAction(ActionEvent event) throws IOException {
        System.out.println("Login button selected");

        /*
         * All Exceptions caught at the GUI not at the other two layers.
         */
        try {
            /*
             * Reference 'bal' to Object BusinessAccessLogin sends parameters to Business Layer
             * calling method login() which passes the TextBox parameters usernameBox and
             * passwordBox down the layers.
             */
            if (bal.login(usernameBox, passwordBox) && (count > 0)) {

                /*
                 * Switch statement so that: user cat 1 -> Reception screen;
                 * user cat 2 'Nurses' -> triage screen ; & user cat 3 'Doctors -> treatment room 
                 */
                switch(bal.staffAccess(usernameBox, passwordBox)){
                case 1:
                    System.out.println("Staff Category ONE");
                    homePageParent = FXMLLoader.load(getClass().getResource("/views/FXMLReceptionistPage.fxml"));
                    homePageScene = new Scene(homePageParent);
                    appStage = (Stage) ((Node) event.getSource()).getScene().getWindow();
                    break;
                case 2:
                    System.out.println("Staff Category TWO");
                    homePageParent = FXMLLoader.load(getClass().getResource("/views/FXMLTriageNurseHomePage.fxml"));
                    homePageScene = new Scene(homePageParent);
                    appStage = (Stage) ((Node) event.getSource()).getScene().getWindow();
                    break;
                case 3:
                    System.out.println("Staff Category THREE");
                    homePageParent = FXMLLoader.load(getClass().getResource("/views/FXMLDoctorAssessmentPage.fxml"));
                    homePageScene = new Scene(homePageParent);
                    appStage = (Stage) ((Node) event.getSource()).getScene().getWindow();
                    break;
                case 4:
                    System.out.println("Staff Category FOUR");
                    homePageParent = FXMLLoader.load(getClass().getResource("/views/FXMLHospitalManagerPage.fxml"));
                    homePageScene = new Scene(homePageParent);
                    appStage = (Stage) ((Node) event.getSource()).getScene().getWindow();
                    break;
                }

                appStage.setScene(homePageScene);
                appStage.show();
                appStage.centerOnScreen();
                appStage.setMaximized(true);

            } else {
                usernameBox.clear();
                passwordBox.clear();
                --count;
                invalidLabel.setText("Sorry, invalid details");

                if (count < 1) {
                    invalidLabel.setText("You have been locked out of system");
                    appStage.close();

                }
                attemptLabel.setText("ATTEMPTS LEFT : " + count);
                System.out.println("ATTEMPTS LEFT : " + count);
            }

        } catch (SQLException e) {
            // TODO Auto-generated catch block
            System.out.println(e.getMessage());
        } catch (Exception e){
            System.out.println(e.getMessage());
            e.printStackTrace();
        }
    } 

This is the code I wish to call when the enter key is pressed. Any help would be greatly appreciated

Upvotes: 1

Views: 553

Answers (1)

Roland
Roland

Reputation: 18415

The question is a bit unclear, but my guess of what you want is the method setDefaultButton.

A default Button is the button that receives a keyboard VK_ENTER press, if no other node in the scene consumes it.

Upvotes: 1

Related Questions