javafx : javafx.scene.layout.AnchorPane cannot be cast to javafx.scene.layout.BorderPane

后端 未结 3 616
闹比i
闹比i 2021-01-15 07:55

hey guys i am knew to javafx and i am trying to cast a BorderPane to an anchronPane, meanwhile an error occured and i don t know what to do, i was following a tutorial so pl

相关标签:
3条回答
  • 2021-01-15 08:37

    Adding as a new post since I am unable to comment on the accepted answer due to lack of points.

    The root element is the lowest element in your document hierarchy.

    When you create a new empty FXML file, open the file for editing in your IDE of choice. Note that the file contains root tags of type <AnchorPane>. This is because the root container of your FXML is a type AnchorPane.

    If you open the FXML file in SceneBuilder and look in the lower left hand side at the document hierarchy you will see the AnchorPane listed as your root element. Click on this element and delete it then save your file. If you view your saved file in your IDE again you should see a completely blank file. This is because you have removed your root element completely.

    Now return to your file in SceneBuilder. Find the BorderPane element in the library in the upper left panel and drag it onto your scene (the middle Scenebuilder panel). Now when you look at the document hierarchy you will see that the BorderPane listed as the root element of your document. Save your file in Scenebuilder and return to your IDE. You will see your element now has a root tag of type <BorderPane>.

    Hope this helps someone out there!

    0 讨论(0)
  • 2021-01-15 08:43

    I was following the same tutorial as well, and encountered the same issue.

    In my case, while creating the RootLayout FXML, I erroneously selected AnchorPane as the root element instead of BorderPane. So, in the initRootLayout() method, it tried to Cast AnchorPane object to BorderPane object. And as mentioned in the previous comment that, AnchorPane and BorderPane extend Pane, it is not possible to cast them with each other!

    Thus, if you select BorderPane as the root element for RootLayout.fxml, it correctly casts the object to BorderPane. Thus, now I am wondering, if this casting is even required?

    I hope your issue is resolved by now. This might be helpful to someone else who encounters this problem!

    0 讨论(0)
  • 2021-01-15 08:47

    BorderPane extends Pane

    AnchorPane extends Pane

    They extend the same class but are different classes and generate different objects that can not be casted into each other.


    Practical example

    Lion extends BigCat

    Tiger extends BigCat

    How would you cast a Lion to a Tiger ?

    0 讨论(0)
提交回复
热议问题