Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can i use Guice in JavaFX controllers?

I have a JavaFX application where I would like to introduce Guice because my Code is full of factorys right now, only for the purpose of testing.

I have one use case where i have a controller class of a certain view. This controller class has a viewmodel and I pass the model to the viewmodel via the constructor of the controller class.

In the controller class I have a contactservice object that provides the edit/save/delete operations. As of now I have an interface of that object and provide an implementation and a Mock. This object can be retrieved by a Factory.getInstance() method.

What I want to do is something like this:

public class NewContactController implements Initializable {
    // ------------------------------------------------------------------------
    // to inject by guice
    // ------------------------------------------------------------------------
    private ContactService contactService;

    @Inject
    public void setContactService(ContactService srv) {
        this.contactService = srv;
    }
    // edit window
    public NewContactController(Contact c) {
        this.viewModel = new NewContactViewModel(c);
    }
    // new window
    public NewContactController() {
        this.viewModel = new NewContactViewModel();
    }

    @FXML
    void onSave(ActionEvent event) {
        //do work like edit a contcat, 
        contactService.editContact(viewModel.getModelToSave());
    }

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        // bind to viewmodel---------------------------------------------------
    }
}

How can I achive this? Is it a good a idea to do something like that? While I was searching for a solution I found fx-guice and similar frameworks but how can i combine these two? Specially how can I let this fields be injected AND instanciate the controller myself or at least give it some constructor args?

like image 775
simonides Avatar asked May 05 '14 11:05

simonides


People also ask

How does JavaFX controller work?

JavaFX controller works based on MVC (Model-View-Controller) JavaFX MVC can be achieved by FXML (EFF-ects eXtended Markup Language). FXML is an XML based language used to develop the graphical user interfaces for JavaFX applications as in the HTML. FXML can be used to build an entire GUI application scene or part of a GUI application scene.

What is JavaFX Gui (Gui)?

JavaFX is considered a Graphical User Interface (GUI) toolkit that helps create desktop applications and games. In this article, different aspects such as features, components, working and examples of JavaFX GUI are shown in detail. This is a guide to JavaFX GUI.

How to set the controller for FXML?

The FXML controller class can bind the Graphical User Interface components declared within the FXML file together and also it makes the controller object becomes a mediator. We can set the controller for FXML in 2 ways: 1. Specifying Inside the FXML File Directly

What is JavaFX MVC?

JavaFX controller works based on MVC (Model-View-Controller) JavaFX MVC can be achieved by FXML (EFF-ects eXtended Markup Language). FXML is an XML based language used to develop the graphical user interfaces for JavaFX applications as in the HTML.


1 Answers

I don't use Guice, but the simplest approach would appear to be just to use a controller factory on the FXMLLoader. You can create a controller factory that instructs the FXMLLoader to use Guice to initialize your controllers:

final Injector injector = Guice.createInjector(...);
FXMLLoader loader = new FXMLLoader(getClass().getResource(...));
loader.setControllerFactory(new Callback<Class<?>, Object>() {
   @Override
   public Object call(Class<?> type) {
       return injector.getInstance(type);
   }
});
// In Java 8, replace the above with 
// loader.setControllerFactory(injector::getInstance);
Parent root = loader.<Parent>load();
like image 80
James_D Avatar answered Oct 11 '22 10:10

James_D