4

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) ????

Victor Laerte
  • 6,446
  • 13
  • 53
  • 102

1 Answers1

7

Update 2023

As noted in the referenced question:

An alternate solution to that presented here is to use MVC, similar to as documented in:

The solution presented here is still perfectly OK, but you might have a bit more flexibility (and a bit more complexity) by adopting the MVC approach instead.


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.

  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. login screen mainview screen

jewelsea
  • 150,031
  • 14
  • 366
  • 406
  • Did you test it? Cause I had to fix somethings to build it, but still don't running. For exemple: FXMLLoader loader = new FXMLLoader("mainview.fxml"); MainViewController controller = loader.MainViewController>getController(); controller.initSessionID(sessionID); scene.setRoot(loader.load()); I think should be: – Victor Laerte Jan 25 '13 at 00:13
  • FXMLLoader loader = FXMLLoader.load(getClass().getResource("MainView.fxml")); MainViewController controller = (MainViewController) loader.getController(); controller.initSessionID(sessionID); scene.setRoot(loader.load()); Even though the last line scene.setRoot(loader.load()); continues to show something wrong (method setRoot in class Scene cannot be applied to given types; required: Parent, found: Object, reason: actual argument Object cannot be converted to Parent by method invocation conversion) Can you help me one more time?? Please. – Victor Laerte Jan 25 '13 at 00:13
  • I just pasted an uncompiled code snippet to show the general approach. I will run everything through a compiler with fxml files and everything to generate new version you could just copy and paste to execute - it will take me some time though. I'll post back when it's done. – jewelsea Jan 25 '13 at 00:35
  • Replaced original psuedo code with a link to an externally hosted compilable sample. – jewelsea Jan 25 '13 at 02:56
  • @jewelsea i find your source examples really really helpfull! :) thx for sharing – Tomas Bisciak Jun 08 '14 at 10:04