28

I have a login screen, and I want to pass the login ID from the LoginController to the MainController, so I can access some functions to change password and whatnot.

I load the controller like this:

FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("fxml/Main.fxml"));     

Parent root = (Parent)fxmlLoader.load();          
Scene scene = new Scene(root); 

stage.setScene(scene);    

stage.show();   

Main.fxml is bounded to the MainController.java. Is there a way I can pass the user ID I need, and access it on the initialize() method of the controller?

Dynelight
  • 2,072
  • 4
  • 25
  • 50
  • http://stackoverflow.com/questions/13003323/javafx-how-to-change-stage/13003602#13003602 , example mentioned in that answer has what you need :) – invariant Jan 17 '13 at 00:25
  • I'm getting lost in your example... Sergey posted this example: http://stackoverflow.com/questions/10134856/javafx-2-0-how-to-application-getparameters-in-a-controller-java-file But I cant seem to get the reference to the previous controller when I load up the new controller. – Dynelight Jan 17 '13 at 01:34
  • logic in that example is , having user data in App(main java class which extends Application) class and then accessing data in all controllers. if its still not clear let me know :) – invariant Jan 17 '13 at 01:55
  • Also I cant get the code from that link, seems broken... – Dynelight Jan 17 '13 at 14:14
  • go to the bottom of this page :) http://www.oracle.com/technetwork/java/javase/downloads/jdk7-downloads-1880260.html – invariant Jan 17 '13 at 14:21

1 Answers1

52

After loading the controller with the FXMLLoader, it is possible to call for members of said controller before the show() method is invoked. One must get the reference to the controller just invoked and call a set() method from there (or access the attribute directly, if defined public).

From the example, let us suppose that the controller associated with Main.fxml is called MainController, and MainController has a userId attribute, defined as an int. Its set method is setUser(int user). So, from the LoginController class:

LoginController.java:

// User ID acquired from a textbox called txtUserId
int userId = Integer.parseInt(this.txtUserId.getText());

FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("fxml/Main.fxml"));     

Parent root = (Parent)fxmlLoader.load();          
MainController controller = fxmlLoader.<MainController>getController();
controller.setUser(userId);
Scene scene = new Scene(root); 

stage.setScene(scene);    

stage.show();   

MainController.java:

public void setUser(int userId){
    this.userId = userId;
}

MainController.java:

//You may need this also if you're getting null
@FXML private void initialize() {
        
    Platform.runLater(() -> {

        //do stuff

    });
        
}
qwerty
  • 810
  • 1
  • 9
  • 26
Dynelight
  • 2,072
  • 4
  • 25
  • 50
  • 13
    Is this actually work? I'm getting user_id always null. – napstercake Apr 13 '14 at 05:28
  • This is working very good: http://stackoverflow.com/questions/14187963/passing-parameters-javafx-fxml – napstercake Apr 13 '14 at 18:34
  • 1
    it works good, but you will receive null the first time you want to print it.... remember.. , you can print it (for example in a buttom you can code the system.out.print ) when your windows loaded . – diego matos - keke Apr 28 '16 at 22:21
  • it works, you just have to wrap where you use the variable with `Platform.runLater` since it takes time for window to load – Hendra Anggrian Sep 11 '17 at 20:12
  • But how we can pass parameter from nex controller to previous controller ? https://stackoverflow.com/questions/46288679/javafxcall-method-from-other-controller-class – Menai Ala Eddine - Aladdin Sep 18 '17 at 23:50
  • 3
    @RicardoGonzales The data is passed after the initialize() method of controller is called. If you check after initialize method you can see the variable. – ozgur Sep 24 '17 at 17:45
  • how do you get to run this in the initialize() method? I always get a severe null error for this. @ozgur – Roger Mar 26 '18 at 02:02
  • 1
    for the initialize() method use Platform.runLater otherwise you'll get null – Harley Sep 27 '18 at 23:52
  • What if I forget to call setUser(..) or any other initialization method? Is there any way to enforce it? – ed22 Feb 02 '21 at 09:59
  • 1
    @user3121518 could you explain in more detail why Platform.runlater helps avoid the issue? – YHStan Jul 13 '21 at 14:36
  • 1
    @YHStan I explained the purpose of `Platform.runLater` usage for this case via my [answer to a related question](https://stackoverflow.com/questions/68363535/passing-data-to-another-controller-in-javafx/68370305#68370305). – jewelsea Jul 13 '21 at 23:26