简体   繁体   中英

JavaFx text field value not getting

I am trying to write a login application using JavaFx. But I am not getting the value from the text filed and password field.

Please see below my code.

The following function is used to set the stage and in it there are two filds username and password.

@Override
public void start(Stage primaryStage) {

    //setStage(primaryStage);


    primaryStage.setTitle("JavaFX Welcome");
    GridPane grid = new GridPane();
    grid.setAlignment(Pos.CENTER);
    grid.setHgap(10);
    grid.setVgap(10);
    grid.setPadding(new Insets(25, 25, 25, 25));

    Text scenetitle = new Text("Welcome");
    scenetitle.setFont(Font.font("Tahoma", FontWeight.NORMAL, 20));
    grid.add(scenetitle, 0, 0, 2, 1);

    Label userName = new Label("User Name:");
    grid.add(userName, 0, 1);

    TextField userTextField = new TextField();
    grid.add(userTextField, 1, 1);

    Label pw = new Label("Password:");
    grid.add(pw, 0, 2);

    PasswordField pwBox = new PasswordField();
    grid.add(pwBox, 1, 2);

    Button btn = new Button("Sign in");
    HBox hbBtn = new HBox(10);
    hbBtn.setAlignment(Pos.BOTTOM_RIGHT);
    hbBtn.getChildren().add(btn);
    grid.add(hbBtn, 1, 4);

    final Text actiontarget = new Text();
    grid.add(actiontarget, 1, 6);

    String username = userTextField.getText();
    String password = pwBox.getText();
    logger.info("from start---userame:"+username+"::pswd:"+password);
    handleEvent(btn,actiontarget,username,password);


    Scene scene = new Scene(grid, 300, 275);
    primaryStage.setScene(scene);


    primaryStage.show();

}

With in this function I am calling another function to validate the username and password. See code below.

public void handleEvent(Button btn,Text actiontarget,String username,String password)
{

    btn.setOnAction(new EventHandler<ActionEvent>() {

        @Override
        public void handle(ActionEvent e) {
            actiontarget.setFill(Color.FIREBRICK);

           boolean result = validateuser(username,password);

           if(result){
              actiontarget.setText("Login Success");
           }
           else{
               actiontarget.setText("Login Fail...");
           }
        }
    });
}

But I am not been able to get the entered username and password in the first function it self.

The problem is that

the user entered values will be available in the button handle event. So I modified the code as below and is working fine now.

Instead of getting the username and password value from the below function, the fields are passed as an argument to the handlEvent() function

@Override
public void start(Stage primaryStage) {

    primaryStage.setTitle(LoginConstants.FX_WELCOME);
    GridPane grid = new GridPane();
    grid.setAlignment(Pos.CENTER);
    grid.setHgap(10);
    grid.setVgap(10);
    grid.setPadding(new Insets(25, 25, 25, 25));

    Text scenetitle = new Text(LoginConstants.TEXT_WELCOME);
    scenetitle.setFont(Font.font("Tahoma", FontWeight.NORMAL, 20));
    grid.add(scenetitle, 0, 0, 2, 1);

    Label userName = new Label(LoginConstants.LABEL_USERNAME);
    grid.add(userName, 0, 1);

    TextField userTextField = new TextField();
    grid.add(userTextField, 1, 1);

    Label pw = new Label(LoginConstants.LABEL_PASSWORD);
    grid.add(pw, 0, 2);

    PasswordField pwBox = new PasswordField();
    grid.add(pwBox, 1, 2);

    Button btn = new Button(LoginConstants.BUTTON_SUBMIT);
    HBox hbBtn = new HBox(10);
    hbBtn.setAlignment(Pos.BOTTOM_RIGHT);
    hbBtn.getChildren().add(btn);
    grid.add(hbBtn, 1, 4);

    final Text actiontarget = new Text();
    grid.add(actiontarget, 1, 6);

    handleEvent(btn,actiontarget,userTextField,pwBox);

    Scene scene = new Scene(grid, 300, 275);
    primaryStage.setScene(scene);

    primaryStage.show();

}

Now the values are taken inside the handle event function.

Here is the handleEvent()

public void handleEvent(Button btn,Text actiontarget,TextField userTextField,PasswordField pwBox)
{

    btn.setOnAction(new EventHandler<ActionEvent>() {

        @Override
        public void handle(ActionEvent e) {

            String username = userTextField.getText();
            String password = pwBox.getText();

            logger.info("from start---userame:"+username+"::pswd:"+password);

            actiontarget.setFill(Color.FIREBRICK);

           boolean result = validateuser(username,password);

           if(result){
              actiontarget.setText(LoginConstants.TEXT_LOGIN_SUCCESS);
           }
           else{
               actiontarget.setText(LoginConstants.TEXT_LOGIN_FAIL);
           }
        }
    });
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM