问题
I have two scenes Login.fxml and MainView.fxml and two diferent controllers LoginController.java and MainViewControler.java
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);
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) ????
回答1:
Use a variation on the solution in Passing Parameters JavaFX FXML.
Setup a LoginManager
which has a reference to both the LoginController
and the MainViewController
.
- 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.


来源:https://stackoverflow.com/questions/14511016/how-can-i-use-a-variable-from-another-controller-in-javafx