在JavaFX控制器之间传递值

时间:2018-06-14 10:29:52

标签: java javafx scenebuilder

我正在尝试在JavaFX场景的控制器之间传递数据。它传递了价值!但它并没有改变我的TextField文本。

这是我的第二个控制器:

public class FXMLStudentController implements Initializable {

    @FXML public TextField firstName;
    @FXML private TextField lastName;
    @FXML private TextField id;


    @Override
    public void initialize(URL url, ResourceBundle rb) {
    }    

    public void find(String user){
        System.out.println(user);
        firstName.setText(user);
    }

}

println方法工作并打印" user",但setText方法不起作用。

这是我的第一个控制者:

public class FXMLLoginController implements Initializable {

    @FXML TextField user;
    @FXML PasswordField pass;
    @FXML Button login;

    public final static Paint trueValue = Paint.valueOf("AEFFBA");
    public final static Paint falseValue = Paint.valueOf("FFC8C8");

    @FXML public void handleLoginBtn(MouseEvent e) throws IOException {
        String username = user.getText();
        String password = pass.getText();
        ConnectionClass connectionClass = new ConnectionClass();
        Connection connection = connectionClass.getConnection();
        try {
            Statement statement = connection.createStatement();
            String sql = "SELECT password FROM students WHERE username ='" + username + "'";
            ResultSet resultSet = statement.executeQuery(sql);
            if(resultSet.next()){
                if(password.equals(resultSet.getString("password"))){

                    Parent studentParent = FXMLLoader.load(getClass().getResource("FXMLStudent.fxml"));
                    Scene studentScene = new Scene(studentParent);
                    Stage app_stage = (Stage) ((Node) e.getSource()).getScene().getWindow();
                    app_stage.hide();

                    try{
                        FXMLLoader loader = new FXMLLoader(getClass().getResource("FXMLStudent.fxml"));
                        Parent root = (Parent) loader.load();

                        FXMLStudentController display = loader.getController();
                        display.find(username);

                        app_stage.setScene(studentScene);
                        app_stage.show();
                    } catch (IOException ex2){}

                } else {
                    pass.setStyle("-fx-control-inner-background: #" + falseValue.toString().substring(2));
                }

            }

        } catch(SQLException ex){
            ex.printStackTrace();
        }

    }

    @Override
    public void initialize(URL url, ResourceBundle rb) {
        // TODO
    }    

}

更新:这里的代码是第一个控制器的整个代码。我该如何编辑它才能更改TextField的文本?

1 个答案:

答案 0 :(得分:0)

我很确定设置文本不起作用的原因是由于这个陈述

FXMLLoader loader = new FXMLLoader(getClass().getResource("FXMLStudent.fxml"));
Parent root = (Parent) loader.load();

FXMLStudentController display = loader.getController();
display.find(username);

这些都没有显示你正在显示第一个fxml,这就是为什么你只应该加载一个而且prinln仍然存在,因为还有另一个父" root"没有显示

也许尝试这个我不确定你的编程是如何工作的,但这是你需要做的事情

if(password.equals(resultSet.getString("password"))){
    try{
        FXMLLoader loader = new FXMLLoader(getClass().getResource("FXMLStudent.fxml"));
        Parent studentParent = loader.load();
        Scene studentScene = new Scene(studentParent);
        Stage app_stage = (Stage) ((Node) e.getSource()).getScene().getWindow();
        FXMLStudentController controller = loader.getController();
        controller.find(username);

        app_stage.setScene(studentScene);
        app_stage.show();
    } catch (IOException ex2){}

}