Skip to content
Advertisement

How to change this JavaFX into using a ControllerFactory

I have looked into using ControllerFactory a lot, to allow this code to be instantiated from a database, and have cross-controller compatibility. But with my original setup different from others I found online, I found it extremely hard to follow along, and use what would fit into my program from theirs. Any advice on where to start?

Current Controller creation –

JavaScript

Controller Example-

JavaScript

Current Controller_Factory-

JavaScript

How would I pass this info with the said controller? (This is not real code I have, but an example of configuration JSON I want to pass with the controller.)

JavaScript

Advertisement

Answer

The controller factory is simply a Callback<Class<?>, Object> whose call(Class<?> type) function takes the class defined in the fx:controller attribute in the FXML file and returns the object to be used as the controller. This is invoked by the FXMLLoader at the time the FXML is loaded.

I think your question is asking if you can use a controller factory to automatically populate controllers with data that’s stored in JSON, which will be read at runtime.

You can do something like this:

JavaScript
JavaScript
JavaScript

Now just define the fx:controller attribute in each FXML in the usual way. E.g. for Buy.fxml do

JavaScript

Then

JavaScript
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement