Sunday, 1 July 2012
Creating JavaFX Controllers Using Guice
I really like Guice Dependency Injection so was pleased to hear that some work has been done to make this interact with JavaFX. The solution presented though, was less than ideal. It had some duplication of configuration where the class name of the controller needed to specified in the FXML as you would expect but also when loading it. After a brief fumble through FXMLLoader's methods I found #setControllerFactory. By passing this wafer thin adapter below as a parameter to #setControllerFactory, any controllers will be instantiated via Guice.
Subscribe to:
Post Comments (Atom)
3 comments:
Nice, thank you.
Good post!
Glad I'm not the only one who wants to use Guice and JavaFX/FXML together... :)
Basically, I use the same approach that you describe, but I decided to create a custom GuiceFXMLLoader class instead and perform initialization of the JavaFX Application object by using a custom GuiceApplication class. That saves me a bunch of lines of code and seems to work pretty nicely.
If you want to check out my code, head over to Github and have a look: https://github.com/cathive/fx-guice
Hi Benjamin
Your project looks like a good starting point if anyone is creating a new JavaFX project with Guice. I have coded almost exactly the same thing in my own work.
Would you be interested in incorporating the code of my second FXML/Guice post? It allows multiple controllers in one FXML control to talk to each other. If so, let me know and I can fork and incorporate the changes for you to pull back.
Cheers
Andy
Post a Comment