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

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

6 Comments

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:
FXMLLoader loader = FXMLLoader.load(getClass().getResource("MainView.fxml")); MainViewController controller = (MainViewController) loader.<MainViewController>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.
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.
Replaced original psuedo code with a link to an externally hosted compilable sample.
Another approach (similar, but with some differences) here.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.