如何在JavaFX中使用另一个控制器的变量 [英] How can I use a variable from another Controller in JavaFX

查看:1949
本文介绍了如何在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.


  1. loginManager 使用 loginController创建登录屏幕并将对自身的引用传递给 loginController

  2. 登录过程中, loginController 通知登录的 loginManager sessionID

  3. loginManager 可创建 MainViewController ,传递 mainViewController sessionID 并用主视图替换场景内容。

  1. The loginManager creates a login screen using the loginController and passes a reference to itself to the loginController.
  2. When login has passed, the loginController notifies the loginManager of the login sessionID.
  3. The loginManager can then create a MainViewController, passing the mainViewController the sessionID and replacing the scene contents with the main view.

这里是一个指向一些示例代码的链接,以演示此方法。

Here is a link to some sample code to demonstrate this approach.

这篇关于如何在JavaFX中使用另一个控制器的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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