Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Java Build a JavaFX Application Design a Better App Grids

Siu KWAN Yuen
Siu KWAN Yuen
Courses Plus Student 2,898 Points

Layout: GridPlane and VBox

Both of them are layouts which are forms of node, but why the instance of a Vbox has getChildren method and GridPlane does not. I thought the rational behind is the same; the label added into node is a supNode of GridPlane, just as the Text instance is a supNode of Vbox.

Can anyone explain if GridPlane has child node and why?

2 Answers

Seth Kroger
Seth Kroger
56,407 Points

According to the GridPane docs does have a getChildren() method inherited from Pane (which is also the same superclass Vbox has).

Siu KWAN Yuen
Siu KWAN Yuen
Courses Plus Student 2,898 Points

yet instances of GridPane is able to add elements without referring to the instances' child while you have to call the reference of Vbox child to add elements.

        GridPane grid = new GridPane();

        VBox box = new VBox();

Can anyone explain why?

Boban Talevski
Boban Talevski
24,793 Points

It's a bit of a confusing concept to me as well, but the only reason that makes sense to me so far, is that when adding elements to a GridPane, you probably want to pass additional parameters for the row and column, while for the VBox you don't. So they (the developers of JavaFX) didn't bother exposing an add method for the VBox.

Which would be really simple though and it would probably look like this if they added it. It will just call getChildren itself, and use the same parameter which we pass to the add method.

public void add(javafx.scene.Node child) {

There's also the fact that when using getChildren on a GridPane, you can't chain the add method we are using which accepts the row and column number, only a "simpler" version of the add method is exposed this way. So, probably they needed to have a more specific implementation of the add method for a GridPane specifically because of the row and column parameters, while such method isn't needed for the VBox and we are left with getting the children collection and adding to it which isn't that big of a deal.

Just guessing here, though, this is my first encounter with JavaFX.