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

查看:23
本文介绍了如何在 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 中,我需要使用 this 对象 (loginGateway) 来获取 JSessionID 并向服务器发出其他请求.但是我怎样才能在另一个控制器类(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,它同时引用 LoginControllerMainViewController.

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天全站免登陆