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 Build a JavaFX Application Design a Better App Separating the Layout

Razvan Cirstea
Razvan Cirstea
2,493 Points

Upcasting to (GridPane) questions

I don't understand the whole "GridPane example= (GridPane) root;" bit . I have so many questions about this :)

  1. Why do we need to go through all this "trouble" ? What is the purpose ?
  2. Why do we then decide we do not need to do so, and we erase that code ?
  3. Why do we upcast to (GridPane) , when we saw in the documentation it is the lowest library of all ?

I am sure I am missing something here ! Hope I'm not missing something ridiculously obvious :)

1 Answer

Doli Harahap
Doli Harahap
6,246 Points

Hi,

  1. Why do we need to go through all this "trouble" ? What is the purpose ?
  2. Why do we then decide we do not need to do so, and we erase that code ? Look at this FXMLLoader.load() https://docs.oracle.com/javase/8/javafx/api/javafx/fxml/FXMLLoader.html#load--

It returns a generic type which is designed so programmer can return any type that defined in the FXML document. You don't have to do that, you can directly catch the Class:

GridPane root = FXMLLoader.load(getClass().getResource("sample.fxml"));

  1. Why do we upcast to (GridPane) , when we saw in the documentation it is the lowest library of all ? It's not like have to be GridPane, it follows your root tag in your FXML.

Hope it helps.