Java Reference
In-Depth Information
Figure 3-6.
The ResolutionAndBindingExample program
Using Multiple FXML Files
Because the result of loading an FXML file is a JavaFX
Node
that can be used in a
Scene
, you are not limited to using
only one FXML file for any
Scene
. You can, for example, break your scene into two or more parts and represent each
part by its own FXML file. Then you can call one of the
load()
methods of
FXMLLoader
on the FXML file of each part,
and assemble the resulting nodes programmatically into your scene.
The FXML file format supports another mechanism to combine separately prepared FXML files together. One
FXML file can include another with an
fx:include
element. The
fx:include
element supports three attributes:
the
source
attribute holds the location of the included FXML file; the
resources
attribute holds the location of the
resource bundle that is used by the included FXML file; and the
charset
attribute holds the charset for the included
FXML file. If the
source
attribute starts with a “
/
” character, it is interpreted as a path in the classpath; otherwise
it is interpreted as relative to the location of the including FXML file. The
resource
and the
charset
attributes are
optional. When they are not specified, their values used for loading the including FXML file are used. The builder
factory and the controller factory used for loading the including FXML file are also used for loading the included
FXML file.
An
fx:id
can be specified for an
fx:include
element. When an
fx:id
is specified, a corresponding field in
the controller of the including FXML file can be specified, and the
FXMLLoader
will inject this field with the result
of loading the included FXML file. Moreover, if the included FXML file has an
fx:controller
specified in its root
element, that included FXML file's controller can also be injected into the including FXML file's controller, provided
a properly named and typed field is available in the including file's controller to receive the injected included FXML
file's controller. In the example application of this section, we use two FXML files to represent the application's UI.
The including FXML file has lines like the following:
<BorderPane maxHeight="-Infinity"
...
fx:controller="IncludeExampleTreeController">
<fx:include fx:id="details"
source="IncludeExampleDetail.fxml" />
and the included FXML has lines like the following:
<VBox maxHeight="-Infinity"
...
fx:controller="IncludeExampleDetailController">
Search WWH ::
Custom Search