Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong>Update</strong></p> <p>In addition to afterburner.fx, also checkout <a href="http://gluonhq.com/labs/ignite/" rel="nofollow noreferrer">Gluon Ignite</a>:</p> <blockquote> <p>Gluon Ignite allows developers to use popular dependency injection frameworks in their JavaFX applications, including inside their FXML controllers. Gluon Ignite creates a common abstraction over several popular dependency injection frameworks (currently Guice, Spring, and Dagger, but we plan at add more as the demand becomes obvious). With full support of JSR-330 Gluon Ignite makes using dependency injection in JavaFX applications trivial.</p> </blockquote> <p>Injection of model objects into controllers is also via @Inject, similar to afterburner.fx.</p> <p><strong>Suggested Approach</strong></p> <p>As you appear to be seeking a dependency injection framework, I think your best option is to use the <a href="http://afterburner.adam-bien.com" rel="nofollow noreferrer">afterburner.fx framework</a>.</p> <p>afterburner.fx provides a way injecting model objects into your JavaFX controllers using the standard Java <a href="http://docs.oracle.com/javaee/6/api/javax/inject/Inject.html" rel="nofollow noreferrer">@Inject</a> annotation. </p> <p><strong>Alternative Dependency Injection Systems</strong></p> <p>Spring is large and complicated and, unless you need a lot of its other functionality for your application, should not be considered due to its complexity.</p> <p>Guice is a lot simpler than Spring and a reasonable pick if you need a dependency injection framework with numerous features such as provider classes. But from the sound of it, you don't need all the features that Guice provides as you just want to a way to pass around singleton instances of objects in your application without explicitly looking them up.</p> <p>So, try out afterburner.fx and see if it fits your needs.</p> <p><strong>afterburner.fx Sample Code</strong></p> <p>Here is sample of injecting a model instance (the <code>NotesStore</code>) into a controller using afterburner.fx. The sample is directly copied from the <a href="http://afterburner.adam-bien.com" rel="nofollow noreferrer">afterburner.fx documentation</a>.</p> <pre><code>import com.airhacks.afterburner.views.FXMLView; public class NoteListView extends FXMLView { //usually nothing to do, FXML and CSS are automatically //loaded and instantiated } public class AirpadPresenter implements Initializable { @Inject // injected by afterburner, zero configuration required NotesStore store; @FXML // injected by FXML AnchorPane noteList; @Override public void initialize(URL url, ResourceBundle rb) { //view constructed from FXML NoteListView noteListView = new NoteListView(); //fetching and integrating the view from FXML Parent view = noteListView.getView(); this.noteList.getChildren().add(view); } } </code></pre> <p><a href="https://github.com/AdamBien/followme.fx" rel="nofollow noreferrer">followme.fx</a> is a basic sample application demonstrating how to use afterburner.fx. I did have a few issues getting followme.fx running straight out of the box due to Maven dependency incompatibilities, so I <a href="https://github.com/jewelsea/followme.fx" rel="nofollow noreferrer">forked it's code</a> and fixed some of the issues which prevented me from using it out of the box.</p> <p><strong>Answers to addition questions from comments</strong></p> <blockquote> <p>So from the NoteStore example, are you saying all I have to do is add the afterburner framework dependency and put @Inject on my model variable?</p> </blockquote> <p>No, you also need to create an associated class that extends FXMLView and instantiate that with a new call (similar to how NotesListView is created in the sample code above). If you are interested in continuing to investigate the afterburner.fx framework, then use the followme.fx project as basis because it provides complete source code for a very simple executable sample using the framework. </p> <blockquote> <p>I tried google guice and got it to work . . . you'll see in the constructor a game settings object is injected manually.</p> </blockquote> <p>I don't think you should have to use the Guice injector manually like that. I think you can <a href="http://docs.oracle.com/javafx/2/api/javafx/fxml/FXMLLoader.html#setControllerFactory%28javafx.util.Callback%29" rel="nofollow noreferrer">set a controller factory</a> on an FXMLLoader instance to initiate the injection. This is how the <a href="https://github.com/AdamBien/afterburner.fx/blob/master/src/main/java/com/airhacks/afterburner/views/FXMLView.java" rel="nofollow noreferrer">FXMLView</a> in afterburner.fx does it. The exact detail of the injection mechanism used in Guice is going to differ from the afterburner.fx mechanism, but I think the broad concept of setting the controller factory remains similar.</p> <p>There is a demo of the set controller factory using FXML and Guice in the answer to: <a href="https://stackoverflow.com/questions/15990777/associating-fxml-and-controller-in-guices-module-configuration">Associating FXML and Controller in Guice&#39;s Module configuration</a>.</p> <p>It's a shame there is not a more straightforward way of doing this which does not cause you so many difficulties.</p> <p>As an inconsequential personal side note, I'm kind of ambivalent on the topic of dependency injection frameworks in general. Sure, they can help, but many times for simple things I'm often OK with a singleton with a getInstance method rather than the more sophisticated framework. Still I do see how in larger projects they can be useful and certainly they are very popular in certain Java frameworks.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload