1
votes

I am trying to make a simple panel to be displayed in a grid which reacts on a click, but no matter what I do javafx seems not able to connect the FXML to the controller class.

I have a realy simple FXML:

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

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

<AnchorPane id="AnchorPane" prefHeight="50.0" prefWidth="50.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="genmatfx.MaterialGridPanelController">
   <children>
      <BorderPane layoutX="-86.0" layoutY="-75.0" onMouseClicked="#handleOnMouseClicked" prefHeight="200.0" prefWidth="200.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
         <center>
            <ImageView fx:id="MatView" onMouseClicked="#handleOnMouseClicked" pickOnBounds="true" preserveRatio="true" BorderPane.alignment="CENTER" />
         </center>
      </BorderPane>
   </children>
</AnchorPane>

And in the controller class (MaterialGridPanelController.java) a simple event to catch the mouse click:

@FXML
public void handleOnMouseClicked(ActionEvent event) {
    Parent.TriggerMaterialUsed(this, DisplayedMaterial);
}

After some research I found out that there are problems using the static loader methods of the FXMLLoader so I already use an instance of it to create the panel like this:

    FXMLLoader loader = new FXMLLoader(getClass().getResource("MaterialGridPanel.fxml"));
    AnchorPane pane = (AnchorPane) loader.load();
    MaterialGridPanelController controller = loader.<MaterialGridPanelController>getController();
    controller.SetMaterial(this, AvailiableMaterials.get(matName));
    GridPane.setHalignment(pane, HPos.CENTER);
    GridPane.setValignment(pane, VPos.CENTER);
    grid.add(pane, col, row);

But whatever I do I get an exception when running the program, telling me the method for the click event can't be found:

Controller method "handleOnMouseClicked" not found.
file:/C:/.../dist/run309258243/GenMatFX.jar!/genmatfx/MaterialGridPanel.fxml:14
  at javafx.fxml.FXMLLoader$Element.processEventHandlerAttributes(FXMLLoader.java:492)
  at javafx.fxml.FXMLLoader$ValueElement.processEndElement(FXMLLoader.java:589)
  at javafx.fxml.FXMLLoader.processEndElement(FXMLLoader.java:2430)
  at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2136)
  at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2028)

The controller works, I did put the ImageView on a private field, so the problem is not that the controller class can't be found. Also when I load the FXML in the newest Scene Builder I can select this method in the drop down of the event, so the Scene Builder seems to be able to connect.

Anyone has an idea?

1

1 Answers

1
votes

A mouse click is not an ActionEvent, it's a MouseEvent, so your handleOnMouseClicked method isn't found because it has the wrong type parameter.

It should be:

@FXML
public void handleOnMouseClicked(MouseEvent event) {
    . . .
}