How to populate a JavaFX Textfield upon Redirection Using a Controller

47 Views Asked by At

Whenever I try to call the goToPage method, I get the following exception. Before using the controller, I was just literally calling the non static setTextField method using that method's class, but I want to ensure the textfields gets populated properly, and I was told to use a controller to do so properly.

Any help is appreciated!

java.lang.ClassCastException: class com.example.randomOtherController cannot be cast to class com.example.fooController(com.example.randomOtherController and com.example.fooController are in module com.example of loader 'app')
public void goToPage(ActionEvent event) throws IOException {
    FXMLLoader loader = new FXMLLoader();
    loader.setLocation(getClass().getResource("foo.fxml"));
    loader.load();

    FooController fooController = loader.getController();
    fooController.setTextFieldText(part);

    // Now redirect
    // Parent root = FXMLLoader.load(getClass().getResource("foo.fxml"));
    Stage stage = (Stage) ((Node) event.getSource()).getScene().getWindow();
    Parent scene = loader.getRoot();
    stage.setScene(new Scene(scene));
    stage.showAndWait();
}
public void setTextFieldText(Part part) {
    foo1TextField = new TextField();
    foo2TextField = new TextField();
    foo3extField = new TextField();
    foo4TextField = new TextField();
    foo5TextField = new TextField();
    foo6TextField = new TextField();

    foo1TextField .setText(part.getName());
    foo2TextField .setText(String.valueOf(part.getMax()));
    foo3extField .setText(String.valueOf(part.getMin()));
    foo4TextField .setText(String.valueOf(part.getId()));
    foo5TextField .setText(String.valueOf(part.getPrice()));
    foo6TextField .setText(String.valueOf(part.getStock()));
}

I've tried calling the setTextFieldText method using its own class and now the controller. If I use the method by itself, no textfields get populated, if I use the controller as seen in this code, I get the exception mentioned above.

0

There are 0 best solutions below