Java Magazine, September/October 2017
ORACLE COM JAVAMAGAZINE SEPTEMBER OCTOBER 2017 55 javafx to events triggered by UI elements defined by its associated view Unlike the main entry point of a JavaFX application where you have to extend a specific class javafx application Application a controller class may be of any type there are also no marker interfaces that need to be implemented This gives you free range to develop your own types and controller hierarchies as needed Lets begin with a simple application that has a UI that comprises a button and a ListView whose contents will be initialized programmatically A new item will be appended to the list every time the button is clicked The FXML definition for such a UI looks like this xml version 10 encoding UTF 8 import javafx scene control Button import javafx scene control ListView import javafx scene layout VBox VBox xmlns http javafx com javafx xmlns fx http javafx com fxml fx controller sample AppController prefHeight 3000 prefWidth 1000 Button text Add item onAction addItem ListView fx id theList VBox There are new features in this file The first is the fx controller attribute applied to the root element of the UI This attribute instructs FXMLLoader to instantiate an object of the specified class and set it as the controller for this view An object instantiated in this way requires its class to have a public no argument constructor The second feature is found in the button definition theres an extra attribute named onAction Take special note of the format of its value its the character followed by an identifier which turns out to be a method defined in the controller class Finally note the usage of fx id on ListView this instructs FXMLLoader to keep an internal reference to the ListView using the attributes name as a variable name This variable will be
You must have JavaScript enabled to view digital editions.