Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing parameters to a controller when loading an FXML [duplicate]

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?

like image 295
Dynelight Avatar asked Jan 17 '13 00:01

Dynelight


People also ask

Can two FXML files have the same controller?

It's possible to use the same controller class for multiple FXML files, but your code will be really hard to follow, and it's a very bad idea. (Also note that using the fx:controller attribute, you will have a different controller instance each time you call FXMLLoader.

How do I specify a controller in FXML?

FXML Controller Classes There are two ways to set a controller for an FXML file. The first way to set a controller is to specify it inside the FXML file. The second way is to set an instance of the controller class on the FXMLLoader instance used to load the FXML document.

How do I add FXML to another FXML?

The <fx:include> tag can be used to include one fxml file into another. The controller of the included fxml can be injected into the controller of the including file just as any other object created by the FXMLLoader . This is done by adding the fx:id attribute to the <fx:include> element.

How do I join two FXML files?

The classic way to instanciate components via FXMLLoader with nested controllers is: FXML first, then controller for each part. With this technique this is: controller first, then FXML for each component. And you won't load FXML in FXML directly, you will import your custom java classes in the FXML.


1 Answers

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      });          } 
like image 147
Dynelight Avatar answered Sep 21 '22 18:09

Dynelight