Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java

raul cortes
raul cortes
5,745 Points

can not be cast to FXML loader (Java)

Hi, Im kind of new to Java FXML and I wanted to use two fxml files for sharing data but when I call the second stage, the console trhows the " BorderPane can not be cast to FXML loader " error. Basically I want to use two controllers (java files) each one with its fxml file, but when I want to render the new Stage, I got the previous error that I mentioned.

I have the following java files.

//The main java file
package sample;

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

public class Main extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception{
        Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
        primaryStage.setTitle("Hello World");
        primaryStage.setScene(new Scene(root, 800, 500));
        primaryStage.show();
    }


    public static void main(String[] args) {
        launch(args);
    }
}
//controller java file for the sample.fxml
public class Controller {
   /* public void metodo(ActionEvent actionEvent) {
        saludar();
        out.println(calcular());
        out.println(lado1+lado2);
    }*/
   @FXML
   private BorderPane pane;
   @FXML
    TextField usuarioMain;
    public void logIn() throws IOException {
        FXMLLoader loader = FXMLLoader.load(getClass().getResource("emergente.fxml"));
        myControl control= new myControl();
        control.captura(usuarioMain.getText());
        control.mostrar();
         pane=(BorderPane)loader.load();
        Scene scene = new Scene(pane, 400, 275);
        Stage stage= new Stage();

        out.print(usuarioMain.getText());
        stage.setTitle("respuesta de login");
        stage.setScene(scene);
        stage.show();
    }
}

The main.java has no problem, but in the Controller Class when I want to render a new Stage, the IDE throws the cast error that I dont know how to handle, because I was googling and did not found how to solve this. I dont know what part of the cast is wrong..I hope you can help me. Its the only that I need to proceed

1 Answer

Alexander Nikiforov
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Alexander Nikiforov
Java Web Development Techdegree Graduate 22,175 Points

It is hard to help, unless you push project on GitHub, and share a link here.

Not a JavaFx expert, but judging by the code, I think the problem is in this line

 FXMLLoader loader = FXMLLoader.load(getClass().getResource("emergente.fxml"));
 pane=(BorderPane)loader.load();

Because you are casting FXMLLoader to BorderPane ....

I can't say more.

Check answer here. The person is also trying to cast improperly

http://stackoverflow.com/questions/27556536/javafx-scene-layout-pane-cannot-be-cast-to-javafx-fxml-fxmlloader

Judging from another post down here

http://stackoverflow.com/questions/10751271/accessing-fxml-controller-class

In the second answer person is doing the following:

Parent root = FXMLLoader.load(me.getClass().getResource("Form.fxml"));
Label lblData = (Label) root.lookup("#lblData");
if (lblData!=null) lblData.setText(strData); 

This way I guess he is casting root.lookUp to Label, an not the loader.load() directly, like you do ...

Here is also post about usage of two controllers I think

http://stackoverflow.com/questions/19342259/how-to-create-multiple-javafx-controllers-with-different-fxml-files

I strongly recommend to find some tutorials online about how to do two Controllers and two scenes, like this one for example

http://javajdk.net/tutorial/multiple-javafx-scenes-sharing-one-menubar/