Java Magazine, September/October 2017
Define Custom Behavior in FXML with FXMLLoader Inject custom behavior into JavaFX applications using FXML ORACLE COM JAVAMAGAZINE SEPTEMBER OCTOBER 2017 54 javafx In the first article in this series I discussed the basics of the FXML format and a handy utility named FXMLLoader which as the name implies reads an FXML resource parses its contents and constructs a SceneGraph based on the definitions found in the FXML file However FXMLLoader can do more than just that especially when it is combined with a common technique of modern Java development dependency injection DI If youre not familiar with FXMLLoader it might be dificult to follow this article without first reading the earlier article The designers of the FXML format recognized that developers would like to tweak some settings using a programmatic approach such as inserting a dynamic set of items to a ListView or a TableView right after the UI has been created but before the user has any chance to interact with the application To perform such a task JavaFX developers must be able define custom behavior that should be called at a specific time during the applications initialization They also need a way to find the target widget the ListView for example on which the customizations will take place This behavior sounds very similar to what modern DI frameworks provide and thats precisely what the designers of FXML allow you to do Lets see how its done The Controller The JavaFX views you saw in the first article were devoid of any application specific behavior and thats a good thing because it keeps responsibilities separated View elements should be responsible for defining how the UI looks but not how it behaves thats the responsibility of the controller part In FXML terms a controller is an object that participates in DI and can react ANDRÉS ALMIRAY
You must have JavaScript enabled to view digital editions.