Is possible to connect two FXML (JavaFX) files to one controller? I can't do this with changing "fx:controller" in each FXML file...
Any ideas?
Is possible to connect two FXML (JavaFX) files to one controller? I can't do this with changing "fx:controller" in each FXML file...
Any ideas?
Yes, you can do this. Although, it can be done, I do not recommend this approach.
Don't place a
fx:controller
attribute in either FXML. Create a new controller and set the same controller into separate FXMLLoader instances.IMO using a shared controller just to share data is not the preferred solution for this.
Instead, either share the data between multiple controllers, for examples of this see:
There is a further example here:
Use the
fx:root
construct instead offx:controller
. It is explained in the Custom Components section of the FXML docs. I have used it in this example for my students if you want a bigger code example.Using this approach, creating views and controllers will be a lot easier and flexible. You will be able to share data between and connect controllers like you would any other objects in your application (for example: by passing data via the constructor or setter methods).
If you're using SceneBuilder you'll simply need to remove the controller reference and check the box "Use fx:root". Then rework your code as shown in the examples.