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

Aaron Coursolle
Aaron Coursolle
18,014 Points

[Solved]What is the reason for my button not respecting grid settings.

The other components are okay, it's only "btn" that is stuck to the left.

I tried to restart IntellJ in case if it was remembering some previous version of my code.

package sample;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Group;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.VBox;
import javafx.scene.text.Text;
import javafx.stage.Stage;

public class Main extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception{
        //Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
        Group root = new Group();
        Text txt = new Text("Sup");
        Label label = new Label("Name:");
        TextField nameFld = new TextField();
        GridPane grid = new GridPane();
        //zero based "spreadsheet" --column, row
        grid.add(label, 0, 0);
        grid.add(nameFld, 1, 0);

        grid.setHgap(20);
        Button btn = new Button();
        grid.add(btn, 1, 1);
        grid.setGridLinesVisible(true);
        btn.setText("Say Sup");
        btn.setOnAction(evt -> System.out.printf("Sup %s! %n",
                nameFld.getText()));
        txt.setY(50);
        VBox box = new VBox();
        box.getChildren().addAll(txt, grid, btn);
        root.getChildren().add(box);
        primaryStage.setTitle("Sup");
        primaryStage.setScene(new Scene(root, 300, 275));
        primaryStage.show();
    }


    public static void main(String[] args) {
        launch(args);
    }
}
Aaron Coursolle
Aaron Coursolle
18,014 Points

I had updated this line incorrectly:

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

the correct line should be:

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