-1

My program is supposed to run my SignIn.fxml on initial run, with the controller named LoginController. However when I run the usual code:

public void showLoginScreen() {
    try {
      FXMLLoader loader = new FXMLLoader(
              getClass().getResource("SignIn.fxml"));
      Parent root = (Parent) loader.load(getClass().getResource("SignIn.fxml"));
       scene = new Scene(root);
      LoginController.initManager(this);
    }catch (IOException ex) {
      Logger.getLogger(LoginManager.class.getName()).log(Level.SEVERE, null, ex);
    }
  }

I get an exception stating

javafx.scene.Scene cannot be cast to javafx.scene.Parent

I cannot understand this, as it has worked for me in past projects.

I have the scene set as a variable in the controller class.

theunie
  • 73
  • 2
  • 12
  • 1
    what do you mean worked in the past ? was any change happened in the .fxml? From the snippet u shared its not really clear what you trying to achieve, but as a proposal, check the [oracle's link](https://docs.oracle.com/javafx/2/get_started/fxml_tutorial.htm) regarding the fxml loader. In the examples the parent is being casted properly, because a rootPane is defined. – AntJavaDev Sep 19 '19 at 09:04
  • It worked in the past for a different program, as I used it previously to build a login screen before the rest of my program runs. I will check oracle's website again, but the previous times I could not find the problem. – theunie Sep 19 '19 at 09:07
  • I have found my problem, do not worry. you were correct, in a way, I have created a scene dynamicaly and in my FXML file, thus creating a conflict, which I resolved. – theunie Sep 19 '19 at 09:14
  • 1
    cool, then place the response and accept it as answer. It might help others having the same issue – AntJavaDev Sep 19 '19 at 09:16

2 Answers2

0

I have checked my FXML file and found the cause of my problem is a conflict in my fxml file having a scene and the dynamically created scene on my controller itself.

theunie
  • 73
  • 2
  • 12
0

Scene is not an Implementation of Parent. Parent are PARENT Nodes that can be places as the ROOT NODE of a Scene. Scene itself is a on type.

When the *.fxml root is a Borderpane or Pane or any other object that implements Node and Parent it will work.

can you show us the *.fxml maybe you create a scene and not a Parent/Node ?

Luxusproblem
  • 1,913
  • 11
  • 23
  • I have added a pane as the root, and it did work, my problem was the fact that I created a scene dynamically and in the fxml itself as well. as stated in my answer, and previous comments. – theunie Sep 19 '19 at 09:23