The Application Class
So that is the front-end, the easy part (so to speak). I put the code to implement the controls in a separate file, and reference it in the application main program (Listing 3). Compare this code to Listing 1, which is what you get when you create the initial Project.
Let’s look at Listing 3 in detail.
Listing 3 – Date Calculator Main Class
When you create your Project as a JavaFX Application you are extending the Application class, in this case by DateCalculator. There are two methods in this class; main() provides the required Java static method to process any run-time arguments (none, in my case), and to create and launch the application thread. The start() method is abstract in the base class and must be overridden to set the DateCalculator primary Stage and Scene. It is the main entry point for the application. The first thing I do is identify the location and name of the SceneBuilder-created FXML file that defines the DateCalculator UI. The FXMLLoader load() method parses and loads the XML content, effectively defining the UI elements, along with their properties and relationships, including the root node of the UI hierarchy. In my case, the root node is of type AnchorPane, since I use an AnchorPane in SceneBuilder as my top-level container. Note my UI Controller class (DateCalculatorUI) is referenced in the FXML file, through the ‘fx:controller="datecalculator.DateCalculatorUI"’ construct. (You can establish the controller in SceneBuilder, see Section 5 of the Getting Started tutorial). Next, the Scene is set, and the Stage’s show() method makes the UI visible.
A side note here – JavaFX is designed to operate in a multi-threaded environment. At run-time, JavaFX initiates the main thread (called the Application thread, appropriately enough). All UI code is executed on this thread. Additional threads may be initiated to run in parallel with the main thread to accomplish other tasks; for example, background processing, timed event handling, and so forth. There are JavaFX classes (Task, Service) and a Worker interface to accomplish this activity, but I don’t use these features in this application, so I’ll leave off with a pointer to the documentation and move on.
Note my reference to the DateCalculatorUI object in establishing the Calculator Controller. The DateCalculatorUI class extends Stage and performs the bulk of my UI processing. I also set up a reference to the appStage object as an instance variable in DateCalculatorUI; I will use this reference to control dialog Stages that are needed to get and save Event information. More on this below.