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 Grids

Siu KWAN Yuen
PLUS
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,414 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();
        grid.add(label,0,0);
        grid.add(textField,1,0);

        VBox box = new VBox();
        box.getChildren().addAll(txt,grid);
        root.getChildren().add(box);*/

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) {
    getChildren.add(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.