You need to add the top level element fx: controller.
Suppose you have a basic fxml file with a right anchor panel with a button like fxml below.
<?xml version="1.0" encoding="UTF-8"?> <?import javafx.scene.control.*?> <?import java.lang.*?> <?import javafx.scene.layout.*?> <AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1"> <children> <Button layoutX="102.0" layoutY="50.0" mnemonicParsing="false" text="Button" /> </children> </AnchorPane>
In this case, your top-level element will be an anchor panel. If you want to use action buttons such as onMouseClicked , you need to specify fxml your controller class in the top-level element (in this case, the binding kernel), as shown below.
<?xml version="1.0" encoding="UTF-8"?> <?import javafx.scene.control.*?> <?import java.lang.*?> <?import javafx.scene.layout.*?> <AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.example.Controller"> <children> <Button fx:id="buttonExample" layoutX="102.0" layoutY="50.0" mnemonicParsing="false" text="Button" /> </children> </AnchorPane>
fx:controller="com.example.Controller" indicates that my control class is the Controller that is in the com.example package.
And your item id should start with fx , as in the example ( fx:id="buttonExample" ).
emin deniz
source share