JavaFX:在控制器之间传递数据始终为null [英] JavaFX: passing data between Controllers is always null

查看:656
本文介绍了JavaFX:在控制器之间传递数据始终为null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想提前道歉,因为之前在将参数直接从来电者传递到控制器,但我遵循了我找到的每一个可能的解决方案,但仍然无法使其正常工作。

I want to apologize in advance, as this is discussed previously in "Passing Parameters Directly From the Caller to the Controller", but I followed every possible solution I found and still I can't get it work.

我很难从一个参数中传递参数控制器到另一个。

I have difficulty in passing a parameter from one controller to another.

具体来说:

LoginController 传递用户名 MainController

当我点击登录按钮 LoginController 设置用户名 MainController 。但是,当加载Main.fxml时用户名为NULL。

When I click login button LoginController sets the username to the MainController. But, when Main.fxml is loaded username is NULL.

为了弄明白,我想问:


  1. MainController initialize()方法被调用?我想当时 LoginController 调用 st.show();

  2. 如果前一个是正确的,那么为什么 MainController 用户名为NULL,因为我们已经使用 LoginController 中设置了它的值> mainController.setUsername(用户名)?

  1. When MainController initialize() method gets called? I suppose by the time LoginController calls st.show(); ?
  2. If the previous is correct, then why MainController username is NULL, since we have already set its value in LoginController using mainController.setUsername(username) ?

任何帮助都将不胜感激。

Any help would be appreciated.

这是我的代码。

LoginController.java

public class LoginController implements Initializable {
    ...
    @FXML TextField username;

    @FXML public void actionLoginButton() {
        ...
        Stage st = new Stage();
        FXMLLoader loader = new FXMLLoader(getClass().getResource("Main.fxml"));
        Region root = (Region) loader.load();

        Scene scene = new Scene(root);
        st.setScene(scene);

        MainController mainController = loader.<MainController>getController();
        mainController.setUsername(username.getText());

        st.show();
    }
    ...
}

MainController .java

public class MainController implements Initializable {
    ...
    String username;

    @FXML Label lblWelcomeUser;

    public void setUsername(String usrname) {
        this.username = usrname;
    }

    @Override
    public void initialize(URL url, ResourceBundle rb) {
        ...    
        lblWelcomeUser.setText("Welcome, " + this.username);
        ...
    }
    ...
}


推荐答案

问题是您拨打用户名的时间。

MainController initialize()方法在执行期间被称为以下声明:

The initialize() method of MainController is called during the execution of the following statement:

Region root = (Region) loader.load();

此时,用户名字段在 MainController 为null,因此欢迎消息中的值报告为null。在 MainController.initialize()方法完成后,您对 setUsername()的调用发生。

At this time, the username field in MainController is null and so its value is reported as null in the welcome message. Your call to setUsername() occurs after the MainController.initialize() method has completed.

通常,使用FXML加载器加载的控制器类的 initialize()方法不应该尝试对其值为的实例字段执行任何操作没有被FXML加载器注入。在调用 initialize()方法时,这些实例字段不会被初始化。

In general, the initialize() method of controller classes loaded with the FXML loader should never try to do anything with instance fields whose values have not been injected by the FXML loader. These instance fields will not have been initialized at the time that the initialize() method is invoked.

这篇关于JavaFX:在控制器之间传递数据始终为null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆