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();
}
}