FXML Directives

There are several directives that FXML uses that are not directly related to a UI element that appears on screen. This sections gives you an explanation of what they are and what they are used for.

<?import?>

The statement you will see most of but hopefully not write by yourself and have it auto generated. Every time you want to use a UI element in your FXML file you have to import it once at the start of the file. Adding the imports can be done automatically for you by your IDE or you can do them yourself. Imports follow the same style as imports in Java:

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.*?>

<fx:define>

Used to instantiate named objects that your primarily reference in other FXML elements. A standard usecase is the ToggleGroup for toggle or radio controls:

<fx:define>
    <ToggleGroup fx:id="aToggleGroup" />
</fx:define>

<ToggleButton text="Button A" toggleGroup="$aToggleGroup" />
<ToggleButton text="Button B" toggleGroup="$aToggleGroup" />

Not the $ in front of the name when it is used. The toggle group can also be referenced by its id within the controller object of this view.

<fx:include>

The second most used directive used in FXML is the include statement used to import another view that is defined in an FXML file. For more information see the next chapter.

Example

<fx:include source="ui/second_ui.fxml" />

<fx:reference>

<fx:copy>

<fx:root>