I want to create a controller that receives data from a controller when initialized, not after that. Various results show data passed after the second controller is created. Thanks for your help!
Asked
Active
Viewed 24 times
-1
-
See “Setting a controller on the FXMLLoader” in the accepted answer to the duplicate. – James_D May 27 '22 at 19:17
-
You can [get the namespace](https://openjfx.io/javadoc/17/javafx.fxml/javafx/fxml/FXMLLoader.html#getNamespace()) and put stuff in it. The FXMLLoader will inject values with matching fx:ids. You can also [define and bind variables in fxml](https://docs.oracle.com/javase/8/javafx/api/javafx/fxml/doc-files/introduction_to_fxml.html#variable_resolution). Other options such as those in the referenced duplicate or (for complex apps) an [MVC appoach](https://edencoding.com/mvc-in-javafx/) coupled with a controller factory for dependency injection are preferable (IMO). – jewelsea May 27 '22 at 19:24
-
I'm not sure I understand where in the controller's life cycle you mean by "when initialized". Do you mean when the controller is instantiated? Do you mean before the `initialize()` method is called? Or during? Or after? Please [edit] your question to clarify, assuming the duplicate doesn't already answer your question. – Slaw May 27 '22 at 21:43
-
Just for reference, to help understand Slaw's confusion, see James' summary of [fxml loader operation](https://stackoverflow.com/questions/69077854/how-to-create-an-array-of-existing-javafx-objects/69081174#69081174) and this info on [controller constructor vs initialize](https://stackoverflow.com/questions/34785417/javafx-fxml-controller-constructor-vs-initialize-method). – jewelsea May 27 '22 at 22:13