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

Jaden Kuhn
Jaden Kuhn
928 Points

Is there a reason my grid is not working?

package jaredProject;

 import javafx.application.Application;
 import javafx.scene.Group;
 import javafx.scene.Scene;
 import javafx.scene.layout.GridPane;
 import javafx.scene.layout.HBox;
 import javafx.scene.layout.VBox;
 import javafx.scene.text.Text;
 import javafx.stage.Stage;

 public class jaredProjectClass extends Application{

@Override
public void start(Stage primaryStage) throws Exception {
    Group root = new Group();
    Text txt = new Text("StyleMe");
    Text txt1 = new Text("StyleMe");
    GridPane grid = new GridPane();
    grid.add(txt, 0, 0);
    grid.add(txt1, 1, 1);
    VBox box = new VBox();
    box.getChildren().addAll(grid,txt,txt1);
    root.getChildren().add(box);
    primaryStage.setTitle("SmileButtons");
    primaryStage.setScene(new Scene(root, 300, 275));
    primaryStage.show();
}

public static void main(String[] args) {
    launch(args);
}
 }

There is my code... when i run the program it makes the new scene and shows this StyleMe StyleMe

it should be StyleMe StyleMe

Or something like that. Why is this happening?

1 Answer

Doli Harahap
Doli Harahap
6,246 Points

First, you did this:

grid.add(txt, 0, 0);

grid.add(txt1, 1, 1);

Which mean you add txt to first column in first row and txt1 to second column and second row, The expected result is:

| StyleMe | |

| | StyleMe |

But, the problem is you add again the test in the vbox.

box.getChildren().addAll(grid,txt,txt1);

Just remove those because you already added them in the Grid.

box.getChildren().addAll(grid);