javafx Define Blocks and


Example

Sometimes a element needs to be created outside of the usual object structure in the fxml.

This is where Define Blocks come into play:

Contents inside a <fx:define> element are not added to the object created for the parent element.

Every child element of the <fx:define> needs a fx:id attribute.

Objects created this way can be later referenced using the <fx:reference> element or by using expression binding.

The <fx:reference> element can be used to reference any element with a fx:id attribute that is handled before the <fx:reference> element is handled by using the same value as the fx:id attribute of the referenced element in the source attribute of the <fx:reference> element.

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.text.*?>
<?import java.lang.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>


<VBox xmlns:fx="http://javafx.com/fxml/1" prefHeight="300.0" prefWidth="300.0" xmlns="http://javafx.com/javafx/8">
    <children>
        <fx:define>
            <String fx:value="My radio group" fx:id="text" />
        </fx:define>
        <Text>
            <text>
                <!-- reference text defined above using fx:reference -->
                <fx:reference source="text"/>
            </text>
        </Text>
        <RadioButton text="Radio 1">
            <toggleGroup>
                <ToggleGroup fx:id="group" />
            </toggleGroup>
        </RadioButton>
        <RadioButton text="Radio 2">
            <toggleGroup>
                <!-- reference ToggleGroup created for last RadioButton -->
                <fx:reference source="group"/>
            </toggleGroup>
        </RadioButton>
        <RadioButton text="Radio 3" toggleGroup="$group" />
        
        <!-- reference text defined above using expression binding -->
        <Text text="$text" />
    </children>
</VBox>