Java Reference
In-Depth Information
handlers are also hooked up. The
load()
method returns the top-level object in the FXML file, which in our example
is a
Group
. This returned
Group
object is assigned to the
rootGroup
variable and is used in subsequent code the same
way as the programmatically created
rootGroup
in Chapter 2 is used. We then call the
getController()
method to
get the controller with its node fields already injected by the
FXMLLoader
. This controller is assigned to the
controller
variable and is used in subsequent code as if we have just created it and assigned its node fields programmatically.
Now that we've finished switching the Stage Coach program from programmatic to declarative UI creation,
we can run it. It behaves just like in Chapter 2. Figure
3-3
shows the program running with the
transparent
command-line argument.
Figure 3-3.
The Stage Coach program run with transparent command-line argument
In this section we touched on all aspects of the FXML design-time and runtime facilities. However, we described
only parts of each facility, barely enough to get our example program going. In the rest of this chapter, we study each
of the facilities in detail.
Understanding the FXML Loading Facility
The FXML file loading facility is made up of two classes, an interface, an exception, and an annotation in the
javafx.fxml
package. The
FXMLLoader
is the class that does the bulk of the work, such as reading and parsing
the FXML file, recognizing processing instructions in the FXML file, and responding with the necessary actions,
recognizing each element and attribute of the FXML file and delegating the object creation tasks to a set of builders,
creating the controller object if necessary and injecting the nodes and other objects into the controller. The
JavaFXBuilderFactory
is responsible for creating builders in response for
FXMLLoader
's requests for builders for a
particular class. The
Initializable
interface can be implemented by controller classes to receive information from
the
FXMLLoader
as in previous versions of JavaFX; however, this functionality has been superseded by the injection
approach, so we do not discuss it. A
LoadException
is thrown if the FXML file contains errors that make it impossible
for the
FXMLLoader
to construct all the objects specified in the FXML file. The
@FXML
annotation can be used in
controller classes to mark certain fields as injection targets and certain methods as event handler candidates.
Search WWH ::
Custom Search