Skip to content

advantageous/guicefx

Repository files navigation

Build Status ##Lightweight Guice integration for JavaFX

This is a simple library for creating a MVP JavaFX application that uses Google Guice.

##Quickstart

###1. Add maven/gradle dependency

If you are using Maven, add the dependency to your pom.xml

    <dependency>
        <groupId>io.advantageous.guicefx</groupId>
        <artifactId>guicefx</artifactId>
        <version>0.2.3</version>
    </dependency>

If you are using Gradle

    compile 'io.advantageous.guicefx:guicefx:0.2.3'

###2. Create an FXML view.

You will need to access the root of your view from your presenter, so make sure you put an fx:id on your root element.

    <VBox fx:id="root" xmlns:fx="http://javafx.com/fxml">
        <children>
            <Label fx:id="myText" text="Hello, world!"/>
        </children>
    </VBox>

###3. Use either the @Presents or @LoadedBy annotations on your controller/presenter classes.

Use the @LoadedBy annotation if your FXML contains a fx:controller attribute.

    @LoadedBy(value = "MyView.fxml")
    public class MyController {
        ...
    }

Use @Presents if your fxml does not contain a fx:controller.

    @Presents(value = "MyView.fxml")
    public class MyPresenter {
        ...
    }

###4. Map an FXML field to your root element and provide an accessor.

You will be injecting your presenters throughout your application and will need this accessor to get the view.

    @FXML
    private VBox root;

    public VBox getRoot() {
        return root;
    }

###5. Create a JavaFXModule just like you would create a Guice module.

    public class MyApplicationModule extends JavaFXModule {

        @Override
        protected void configureFXApplication() {
            //Do your Guice config here
        }
    }

###6. Initialize your Guice module in the application entry-point and inject your presenter.

    public class MyApplication extends Application {

        @Inject
        private MyPresenter myPresenter;

        @Override
        public void start(final Stage stage) throws Exception {
            Guice.createInjector(new MyApplicationModule()).injectMembers(this);
            stage.setScene(new Scene(myPresenter.getRoot()));
            stage.show();
        }

    }

###7. Bind your entry-point class. (Optional, but recommended)

The entry-point class does a injectMembers on itself, so you may wonder why you would want to also add it in the binder. The reason that it is here is so when Guice constructs it's graph of dependencies, it will throw an nice readable error if there is a problem in the entry-point class. Without this bind here, if there were injection problems in the entry point, you would get an ugly exception when you did the injectMembers.

    public class MyApplicationModule extends JavaFXModule {

        @Override
        protected void configureFXApplication() {
            bind(MyApplication.class);
        }
    }

####A complete working example can be found in the examples folder of the project.