How to use a variable from another controller in JavaFX

I have two scenes Login.fxml and MainView.fxml and two different controllers LoginController.java and MainViewControler.java

In LoginController, I do the whole process to log in and get the JSessionID value and save it in an object, as shown below:

loginGateway = loginGateway(gateway); 

Now in MainViewController I need to use this object (loginGateway) for getJSessionID and make other requests to the server. But how can I use this object in another controller class (MainViewController.java) ????

+4
source share
1 answer

Use the solution option in Passing JavaFX FXML Parameters .

Install a LoginManager that has a link to both LoginController and MainViewController .

  • LoginManager creates a login screen using LoginController and passes the link to itself to LoginController .
  • When the login has passed, the LoginController notifies the LoginManager of the sessionID entry.
  • LoginManager can then create the MainViewController by passing the MainViewController sessionID and replacing the contents of the scene with the main view.

Here is a link to sample code to demonstrate this approach. login screenmainview screen

+7
source

All Articles