To structure your FXML better, you can use fx:define to separate usage and declaration of various elements. For example, the preceding examples with includes can be rewritten by defining the include first and using it later through the $ prefix. Refer to the following code snippet:
<?import javafx.scene.layout.*?><?import javafx.scene.control.*?><HBox xmlns:fx="http://javafx.com/fxml" fx:controller="chapter4.includes.FirstController"> <fx:define> <fx:include fx:id="myLabel" source="MyLabel.fxml"/> </fx:define> <children> <StackPane children="$myLabel"> </children></HBox>
Another common use case for define is setting a toggle group for radio buttons:
<?import javafx.scene.layout.*?><?import javafx.scene.control.*?><HBox ...