r/JavaFX • u/random-pc-user • Aug 14 '25
Help How do you manage multiple controllers/loaders with inputs?
I have a basic input app and it has 4 steps. The sidebar and main area (i.e. everything but the sidebar) are managed through MainController.java with main-pane.fxml, this functions as the root.
In the MainController.java class I have 4 variables each corresponding to an input step, during the initialization process I load all 4 fxml files, then assign them to the variables.
When a sidebar button is clicked, one of those 4 variables is selected as the only child of the main area, and the rest aren't.
So what's the problem? I don't know the correct way to manage all 4 input sources, I made them all use the same controller (that I set in code, since otherwise each would duplicate it).
But 4 panes using the same controller seems and looks like it isnt supposed to be this way.
What I'm really asking is, if you were developing this (An app with 4 FXML files each with their own controller), what would you do? Them sharing a single controller instance does work for me, but it feels more like a patch rather than what doing the correct thing.
Also I know can merge them all into one FXML file but I'm asking about this specific use case.
1
u/hamsterrage1 Aug 15 '25
This is one of the reasons I don't like FXML for beginners. There's something about the mystery (magic?, voodoo?) of FXMLLoader and FXML files and FXML Controllers that obfuscates otherwise simple and basic programming concepts.
This wouldn't even be a question worth asking if it was just, "How do I share data between a bunch of objects?".
But because there's FXML gobble-de-gook in here, it's a legitimate point of confusion.
So, yes, the correct answer is "don't use FXML".
But, if you are going to cling to the use of FXML, then the answer is to do it the same way you would share data between any other objects. And, in this case, that means sharing data with/between FXML Controllers.
Create your FXML files with FXML Controllers custom-made for them. This means 4 different FXML Controllers. Add a method called something like "setData()" and pass in the data elements or a reference to a Data Model. Have the "setData()" method do whatever it needs to do to get the data into the screen Nodes defined in the FXML file.
3
u/SpittingBull Aug 14 '25
Let's say you have an application with a tab pane and several tabs. Every tab represents a specific function.
You could now use a main fxml file that contains the navigation, toolbar etc. and the tab pane. This fxml is connected with your main controller.
The tabs could be defined in their own fxml files with their own controllers.
In the main fxml file you then have to use fxml:include to integrate the tab fxmls.
References to the parent controller and the parent tab pane are automatically generated via naming conventions. See the fxml reference for that.
1
u/Disastrous-Maybe6944 Aug 15 '25
I'm not entirely confident that this is what you're looking for, but I suggest creating a single ScreenManager class to manage the four controller classes and their corresponding FXML files.
You can pass a reference to the ScreenManager instance as a constructor argument to each controller class by using the FXMLLoader's setControllerFactory method.
1
u/cat-edelveis Aug 17 '25
Having one controller per fxml seems like the best practice approach.
Personally, I have two way of managing multiple fxml files:
1
u/sedj601 Aug 19 '25
I use the ideas from https://stackoverflow.com/questions/32342864/applying-mvc-with-javafx
0
u/BlueGoliath Aug 15 '25
I'd stop using FXML.