How to use a controller in FXML. The control binds the user interface, defined in the FXML file, to it’s logic and extends either a control or a layout. The controller is a class whose object is created by FXML. It is used to initialize the UI elements. Only one controller can be included for each FXML document. The controller is instantiated by the FXML loader class in the main application.
Here is an example. Parent root equals FXMLLoader.load(getClass().getResource The string inside the Get Resource must match the name exactly to the name in your project for the FXML file. So when this command is executed it will automatically instantiate a controller object. The controller must have a public constructor that does not take any arguments.
If it does not exist, the FXMLLoader will not be able to instantiate it, which will throw an exception at load time. The FXMLLoader automatically looks for accessible instance variables of the controller. In addition to these variables, if the FXML document has any even handlers, they can be programmed as public methods in the controller as well. If the name of an accessible instance variable matches the FX ID attribute of an element in the FXML file, the object reference from FXML is automatically copied into the controller instance variable.
This feature makes the references of UI elements in FXML available to the controller. The controller can then use them later, such as binding them to a model. Here is an example. This is an excerpt from the controller file. Public class FXMLDocumentController implements Initializable. There’s some code above the @FXML and some below private Label label, but I wanted to concentrate on those two statements.
The use of the @FXML annotation declares that the FXMLLoader can access the member even if it is private. This annotation can be used on fields and methods. Now, let’s look at the corresponding FXML code. In the FXML file, we also have a label, which is the name of a class in Java FX. We have an FX ID that matches the name of the private Label label in our controller and we’re setting the text to enter your name.
Now, these two fields are bound together. It is recommended to use the Initializable interface to initialize elements in your program. Since this is an abstract class, you must override the initialize method. Use this method to make sure all FXML controls are initialized and loaded successfully. This method is called automatically after the loading of the FXML document is complete. To expose any public attributes, we can use Get and Set methods to control data and action methods that need implementation logic.
To recap, @FXML binds FXML attributes with defined actions. The FXMLLoader is used in the main application file to load the FXML file, which also instantiates an object of the controller. It parses the FXML file and constructs an object graph. And finally, the controller file contains the logic required by the view. As you can see, the controller and the FXML file go hand in hand and are very powerful tools in our toolbox of Java FX.