fx:id attribute
The fx:id
attribute is used to name elements in an FXML document. If the document has a code-behind class, the FXML compiler generates a protected field with the same name to make the named element accessible from Java code.
The fx:id
attribute also sets the Node.id
property to the same value.
Usage
com/sample/MyControl.fxml
<StackPane xmlns="http://javafx.com/javafx" xmlns:fx="http://jfxcore.org/fxml/2.0"
fx:class="com.sample.MyControl">
<Button fx:id="myButton1"/>
</StackPane>
com/sample/MyControl.java
public class MyControl extends MyControlBase {
MyControl() {
initializeComponent();
// The named element "myButton1" can be referenced in the code-behind class.
// Note that the value of "myButton1" is set by initializeComponent(), so it is
// only available after the method was invoked.
myButton1.setText("My Button");
}
}