Including other FXML files

You can include one FXML in another, as shown in the following code:

<?import javafx.scene.layout.*?><HBox xmlns:fx="http://javafx.com/fxml">    <children>       <fx:include source="my_fields.fxml"/>       <TextField fx:id="textField" />       <Button fx:id="button" text="Button" />    </children></HBox>

Here, my_fields.xml contains all the listed data:

<?import javafx.scene.control.*?><Label text="Label" />

Note that in this document, Label is a root element for simplicity. But, if you want to have several controls, you'll have to introduce a layout manager to handle them—there can be only one root element in the FXML.

But, what to do if the included FXML has its own Controller? They are called nested controllers and can be handled through ...

Get Mastering JavaFX 10 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.