I have been experimenting with the FXMLLoader
and using the setControllerFactory
method using a custom Callback<P,R>
implementation.
The ORACLE documentation says the following:
An implementation might return a null value to indicate that it does not or cannot create a controller of the given type; in this case, the default controller construction mechanism will be employed by the loader.
The result I want to achieve is that I can use a dependency injection framework to create any controllers that require parameters but I will let the FXMLLoader
load any controllers that do not require parameters.
So if I have the following simple FXML file which uses the ViewController
class which accepts no parameters...
<StackPane fx:id="pane"
xmlns:fx="http://javafx.com/fxml"
fx:controller="my.package.ViewController">
</StackPane>
and I use the following simple controller factory implementation to signal to the FXMLLoader
that I want it to manage the construction of the controller in this case...
loader.setControllerFactory(new Callback<Class<?>, Object>(){
@Override
public Object Call(Class<?> type) {
return null; // Let the FXMLLoader handle construction...
}
});
after calling the load()
method my Initialise method in the ViewController
class is never called (I have verified this with a breakpoint).
If I change my controller factory implementation to return an instance of the ViewController
class then everything works as expected.
Can anyone help me to clear up my confusion? Am I using the Callback
interface incorrectly or is the ORACLE documentation incorrect?
javafx does the following in FXMLLoader:
try {
if (controllerFactory == null) {
setController(ReflectUtil.newInstance(type));
} else {
setController(controllerFactory.call(type));
}
} catch (InstantiationException exception) {
throw new LoadException(exception);
} catch (IllegalAccessException exception) {
throw new LoadException(exception);
}
so, yes, the oracle tutorial is incorrect.