I have problems with my code and surprisingly dont know why. I have already read a few answers to similar questions but they dont seem to fit or have been tried already.
My Controller class is like that:
CTRLUsermenu.java
package user;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
public class CTRLUsermenu implements Initializable{
@FXML
private Button change_user;
@FXML
private Button admin_users;
@FXML
private static void changeUser(ActionEvent e) {
// System.out.println("test");
}
@FXML
private static void adminUsers() {
// System.out.println("test");
}
@Override
public void initialize(URL location, ResourceBundle resources) {
}
}
The FXML-File: usermenu.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.layout.VBox?>
<VBox alignment="CENTER" spacing="20.0" fx:controller="user.CTRLUsermenu" xmlns="http://javafx.com/javafx/17" xmlns:fx="http://javafx.com/fxml/1">
<children>
<Button fx:id="change_user" mnemonicParsing="false" prefWidth="160" text="Change User" onAction="#changeUser"/>
<Button fx:id="admin_users" mnemonicParsing="false" prefWidth="160" text="User administration" onAction="#adminUsers"/>
<Button cancelButton="true" mnemonicParsing="false" prefWidth="160" text="Cancel" />
</children>
</VBox>
And as the title says i get the errormessage:
javafx.fxml.LoadException: Error resolving onAction='#changeUser', either the event handler is not in the Namespace or there is an error in the script.
Without the onAction property the code runs just fine and opens a new window with three buttons.
Common problems that dont seem to be the case here:
- FXML-Annotations
- Imported AWT-Classes
- Initializable Controllerclass
- Misspelled files, classes, methods
- Wrong parameter type in method
EDIT: As i dont have static fields, i dont see the relation to the proposed answer.
Edit2: Ok youre right jewelsea, this also applies to static methods. Question answered.
Edit3: closing quote was lost somewhere on the way.