Skip to content

Instantly share code, notes, and snippets.

@andytill
Created July 1, 2012 17:53
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andytill/3029103 to your computer and use it in GitHub Desktop.
Save andytill/3029103 to your computer and use it in GitHub Desktop.
Creating JavaFX Controllers using Guice
/**
* A JavaFX controller factory for constructing controllers via Guice DI. To
* install this in the {@link FXMLLoader}, pass it as a parameter to
* {@link FXMLLoader#setControllerFactory(Callback)}.
* <p>
* Once set, make sure you do <b>not</b> use the static methods on
* {@link FXMLLoader} when creating your JavaFX node.
*/
class GuiceControllerFactory implements Callback<Class<?>, Object> {
private final Injector injector;
public GuiceControllerFactory(Injector anInjector) {
injector = anInjector;
}
@Override
public Object call(Class<?> aClass) {
return injector.getInstance(aClass);
}
}
@kaisellgren
Copy link

What is Injector? I can't find it in the JavaFX packages.

@gontard
Copy link

gontard commented Feb 12, 2013

An Injector is a guice class.

@gleb-s
Copy link

gleb-s commented Feb 6, 2017

Thank you, man! Very helpful

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment