0

javaFx: Error resolving onAction='#switchToScene2', either the event handler is not in the Namespace or there is an error in the script.

Solve the problem javaFx: Error resolving onAction='#switchToScene2', either the event handler is not in the Namespace or there is an error in the script.

Main.java

package application;
    

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.stage.Stage;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.image.Image;




public class Main extends Application {
    @Override
    public void start(Stage stage) {
        try {
            Parent root = FXMLLoader.load(getClass().getResource("Scene1.fxml"));
            Scene scene = new Scene(root);
            Image icon = new Image("app1.png");
            stage.getIcons().add(icon);
            stage.setScene(scene);
            stage.show();
        } catch(Exception e) {
            e.printStackTrace();
        }
    }
    
    public static void main(String[] args) {
        launch(args);
    }
    
}

controller.java

package application;

import java.awt.event.ActionEvent;
import java.io.IOException;

import javafx.fxml.FXMLLoader;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;



public class SceneController {
    
     private Stage stage;
     private Scene scene;
     private Parent root;
      
    
     @SuppressWarnings("unused")
    private  void switchToScene1 (ActionEvent event) throws IOException {
    root = FXMLLoader.load(getClass().getResource("Scene1.fxml"));
    stage = (Stage)((Node)event.getSource()).getScene().getWindow();
    scene = new Scene(root);
    stage.setScene(scene);
    stage.show();
    
    
    
} 
    @SuppressWarnings("unused")
    private  void switchToScene2 (ActionEvent event) throws IOException  {
    stage = (Stage)((Node)event.getSource()).getScene().getWindow();
    Parent root = FXMLLoader.load(getClass().getResource("Scene2.fxml"));
    //stage = (Stage)((Node)event.getSource()).getScene().getWindow();
    scene = new Scene(root);
    stage.setScene(scene);
    stage.show();
    
    
}
         
 }
package application;

import java.awt.event.ActionEvent;
import java.io.IOException;

import javafx.fxml.FXMLLoader;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;



public class SceneController {
    
     private Stage stage;
     private Scene scene;
     private Parent root;
      
    
     @SuppressWarnings("unused")
    private  void switchToScene1 (ActionEvent event) throws IOException {
    root = FXMLLoader.load(getClass().getResource("Scene1.fxml"));
    stage = (Stage)((Node)event.getSource()).getScene().getWindow();
    scene = new Scene(root);
    stage.setScene(scene);
    stage.show();
    
    
    
} 
    @SuppressWarnings("unused")
    private  void switchToScene2 (ActionEvent event) throws IOException  {
    stage = (Stage)((Node)event.getSource()).getScene().getWindow();
    Parent root = FXMLLoader.load(getClass().getResource("Scene2.fxml"));
    //stage = (Stage)((Node)event.getSource()).getScene().getWindow();
    scene = new Scene(root);
    stage.setScene(scene);
    stage.show();
    
    
}
         
 }
package application;

import java.awt.event.ActionEvent;
import java.io.IOException;

import javafx.fxml.FXMLLoader;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;



public class SceneController {
    
     private Stage stage;
     private Scene scene;
     private Parent root;
      
    
     @SuppressWarnings("unused")
    private  void switchToScene1 (ActionEvent event) throws IOException {
    root = FXMLLoader.load(getClass().getResource("Scene1.fxml"));
    stage = (Stage)((Node)event.getSource()).getScene().getWindow();
    scene = new Scene(root);
    stage.setScene(scene);
    stage.show();
    
    
    
} 
    @SuppressWarnings("unused")
    private  void switchToScene2 (ActionEvent event) throws IOException  {
    stage = (Stage)((Node)event.getSource()).getScene().getWindow();
    Parent root = FXMLLoader.load(getClass().getResource("Scene2.fxml"));
    //stage = (Stage)((Node)event.getSource()).getScene().getWindow();
    scene = new Scene(root);
    stage.setScene(scene);
    stage.show();
    
    
}
         
 }
  • 3
    Wrong import - never-ever import anything from java.awt! [JavaFx, Problems with @FXML](https://stackoverflow.com/questions/31414346/javafx-problems-with-fxml) – kleopatra Dec 01 '22 at 12:07
  • 3
    Additionally, if the methods are private, they need to be annotated `@FXML`. – James_D Dec 01 '22 at 12:54
  • 2
    You've posted the controller class three times. Did you mean to post the FXML files? – James_D Dec 01 '22 at 12:55

0 Answers0