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

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);