JavaFX:在控制器之间传递数据始终为null [英] JavaFX: passing data between Controllers is always 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.
为了弄明白,我想问:
- 当 MainController
initialize()
方法被调用?我想当时 LoginController 调用st.show();
? - 如果前一个是正确的,那么为什么 MainController
用户名
为NULL,因为我们已经使用LoginController 中设置了它的值> mainController.setUsername(用户名)
?
- When MainController
initialize()
method gets called? I suppose by the time LoginController callsst.show();
? - If the previous is correct, then why MainController
username
is NULL, since we have already set its value in LoginController usingmainController.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屋!