Giter Site home page Giter Site logo

guicefx's Introduction

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.

guicefx's People

Contributors

sailorgeoffrey avatar

Stargazers

 avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Forkers

panmingzhi815

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.