Java Magazine, September/October 2017
ORACLE COM JAVAMAGAZINE SEPTEMBER OCTOBER 2017 61 javafx There you have it The controller instance is fetched from the DI container This fetch operation varies from framework to framework and thus is omitted from the example You can even apply a lambda expression to define a simple ControllerFactory Given that the AppController class is now managed by the DI container you can leverage other features such as methods annotated with @ PostConstruct thus allowing further customizations on the controller Just remember that any methods annotated with @ PostConstruct will be invoked before any @ FXMLannotated fields are injected into the controller instance As a side efect of this setup the value of fx controller becomes irrelevant However it is still a good idea to specify the actual type of the controller supplied by the DI container because your IDE will give you hints on the types and names of widgets that can be injected using the @ FXML annotations as well as the names of FXML provides a declarative way to event handlers supplied by the controller As an define JavaFX UIs but it cant be used alternative you could set the controller property to define behavior directly in FXMLLoader but I decided to show the factory approach because that might come in handy for lazily calculating the value Up to this point youve seen views and controllers But as you know there are several design patterns that can be used to design a UI application most of them being a variation of the model view controller MVC pattern such as Model View Presenter and Model View View These patterns make an explicit diferentiation of each member by responsibility which keeps the implementation neat and tidy making it easy to find your way around the application You might be wondering where the model could be in the examples youve seen The answer is that its implicit inside the controller If you were to keep a state such as an ObservableList String of all items its likely that the first place to put it would be in the controller itself For a small application such as this one this decision might not be wrong but as
You must have JavaScript enabled to view digital editions.