如何在JavaFX中使用另一个控制器的变量 [英] How can I use a variable from another Controller in JavaFX
问题描述
我有两个场景Login.fxml和MainView.fxml和两个不同的控制器LoginController.java和MainViewControler.java
I have two scenes Login.fxml and MainView.fxml and two diferent controllers LoginController.java and MainViewControler.java
在LoginController中,我执行整个过程登录和获取JSessionID的值并将其存储在对象中,如下所示:
In LoginController I do the whole process to login and get the value of JSessionID and store it in a object, like below:
loginGateway = loginGateway(gateway);
现在在MainViewController中,我需要使用这个对象(loginGateway)getJSessionID,服务器。但是如何在另一个控制器类(MainViewController.java)中访问此对象?
Now in MainViewController I need to use the this object (loginGateway) to getJSessionID and make other requests to the server. But how can I acess this object in another Controller Class (MainViewController.java) ????
推荐答案
解决方案,传递参数JavaFX FXML 。
设置一个 LoginManager
,它引用了 LoginController
和 MainViewController
。
Setup a LoginManager
which has a reference to both the LoginController
and the MainViewController
.
-
loginManager
使用loginController创建登录屏幕
并将对自身的引用传递给loginController
。 - 登录过程中,
loginController
通知登录的loginManager
sessionID
。 -
loginManager
可创建MainViewController
,传递mainViewController
sessionID
并用主视图替换场景内容。
- The
loginManager
creates a login screen using theloginController
and passes a reference to itself to theloginController
. - When login has passed, the
loginController
notifies theloginManager
of the loginsessionID
. - The
loginManager
can then create aMainViewController
, passing themainViewController
thesessionID
and replacing the scene contents with the main view.
这里是一个指向一些示例代码的链接,以演示此方法。
Here is a link to some sample code to demonstrate this approach.
这篇关于如何在JavaFX中使用另一个控制器的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!