Java Reference
In-Depth Information
Figure 3-7. The IncludeExample program
Creating Custom Components Using fx:root
The fx:include element allows us to attach one FXML file into another FXML file. Similarly, the fx:root element
allows us to attach an FXML file to a Node that is provided in code. The fx:root element must be the top-level element
in an FXML file. It must be supplied with a type attribute, which determines the type of the Node that needs to be
created in code to load this FXML file.
In its simplest form, you can change any one of the earlier FXML files' top-level element from
<SomeType ...
to
<fx:root type="some.package.SomeType" ...
and instantiate SomeType in code and set it as the root in the FXMLLoader before loading the FXML file, like the
following:
SomeType someType = new SomeType();
fxmlLoader.setRoot(someType);
fxmlLoader.load();
The next example goes one step further. It defines a class that extends the fx:root type of the FXML file, and serves
as both the root and the controller of the FXML file. It loads the FXML file in its constructor, and uses the initialize()
method to set up the desired relationships between the nodes that are constructed in the FXML file. This class can then
be used as if it is a native JavaFX node. Classes that are constructed this way are called custom components .
 
Search WWH ::




Custom Search