0

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.

jewelsea
  • 150,031
  • 14
  • 366
  • 406
thebert
  • 35
  • 8
  • 2
    Marked duplicate references static fields, but, probably, static methods won't work either and static controller methods aren't a good idea in any case. – jewelsea Mar 27 '23 at 08:22
  • 1
    You also have a syntax error `onAction="#adminUsers/>` -> no closing quote. – jewelsea Mar 27 '23 at 08:28
  • 1
    unrelated, but I also advise following [Java naming conventions](https://www.oracle.com/java/technologies/javase/codeconventions-namingconventions.html). – jewelsea Mar 27 '23 at 08:31
  • 1
    Forgot that i can comment my own entries^^ Thank you, the static modifier was the source. Non-static methods just work fine. – thebert Mar 27 '23 at 08:31
  • I updated the referenced duplicate to mention static methods in addition to static fields. I guess could have added an independent answer here, but the root cause is quite similar, so I think it is best to have all the info in one place. – jewelsea Mar 27 '23 at 23:00

0 Answers0