I'm building a JavaFX app and I've discovered the joy of
FXMLloader.setControllerFactory(setControllerFactory(Callback<Class<?>,Object> controllerFactory)
I went off a searched example to get my code working:
loader.setControllerFactory(c -> {
return new MyController(dialog, seed);
});
It's beautifully simple and clean and now I can define intended controllers in my FXML files but I can construct the controllers with much more complexity/seeding that doesn't happen during injection. Sorry, I'm not really understanding what took place there. I'm sure that's a lambda or an anonymous class. Could somebody please demystify this for me? I'm not sharp with lambdas. They always look attractive and the scoping access for local variables is always handy but they confuse the heck out of me.
In particular... The setControllerFactory
needs to be passed an instance X of Callback<P,R>
with generics P->Class<?>
and R->Object
so that something can do X.call(Class<?>)
which returns an Object
?
What/where did I define a Class<?>
thing here? How would I code this without any lambdas/anonymous classes?
` there is a method `public R call(P param);`). I.e. in your code the parameter `c` is of type `Class>`. You don't instantiate `Class>` (which I guess is what you mean by "define"???) because it's created by whoever calls the method you define by the lambda; in this case that is the `FXMLLoader`.
– James_D Apr 14 '22 at 16:53