A lot of blog posts are related to CDI for JavaFX applications.
But why do you need to add a complex mechanism to perform simple task, you will answer that all these magical things will simplify developer’s life.
You ‘re wrong, magic things that add additional stuff are not so useful
Why should we inject loader or other utilities classes to do a common task ?
For example when you want to load a fxml file to use the root node into your Scene, you can think that it could be pretty cool to inject the FXMLLoader to load the stuff.
A developer will complain about this technique because he will prefer to not manage this loading phase which is only related to technical stuff.
JRebirth provides a more useful way to do it by using JRebirth Resources way.
Let’s see how to retrieve a node from an FXML file:
Define your FXML file resource:
FXMLItem ROOT_EMBEDDED_FXML = Resources.create( new FXML("org.jrebirth.showcase.fxml.ui.embedded", "Embedded") );
The best way is to group all FXML resources into a custom interface.
This FXML & its resource bundle will be converted to an FXMLComponent and cached into JRebirth internal engine.
To add this node into a BorderPane just write this:
this.borderPane.setCenter( ROOT_EMBEDDED_FXML.get().getNode() );
The get() method will retrieve the resource from internal cache; some unused resources can be released if memory usage is constrained; if the resource hadn’t been create or had been released it will process the FXML file to generate a FXMLComponent.
The getNode() of FXMLComponent class is used to retrieved the FXML root node
This FXMLComponent let access to the root fxml node and also the automatically attached FXMLController (getController()).
What else ?
Getting a FXML node so quickly without memory is cool but how about dealing with a lot of FXML files in an huge stack of different views ?
All combinations are possible by using JRebirth wCS-Mvc pattern which provides custom classes that wrap FXML files in different way.
you can have a look to FXML showcase application here: www.jrebirth.org/showcases/FXML.html
Code is available here: github.com/JRebirth/JRebirth/tree/master/org.jrebirth.af/showcase/fxml
As you can see JRebirth’s main goal is to simplify developer life to help us to be more efficient and business-ready.
We will see later how to use Guice library to plug different components on the fly according to your guice configuration by using the customized ComponentFactory