Ryan Button
Ryan Button

Reputation: 11

How to fix Labels on GridPane stacking on index (0,0)? It seems to be ignoring my input to the function

I am trying to make a basic login screen in javafx using the GridPane layout. The issue is that all the labels I put onto the grid are going to position (0,0) seemingly ignoring my values for the column and row indices.

I've tried putting extreme values for the indices into both function calls and nothing moves. It seems to ignore any indices I give it and defaults to (0,0).

GridPane setup:

GridPane grid = new GridPane();
grid.setPadding(new Insets(10, 10, 10, 10));
grid.setVgap(10);
grid.setHgap(10);

The function calls for the Labels:

// Name label
    Label nameLabel = new Label("Username:");
    GridPane.setConstraints(grid, 0, 0);     

// Password label
   Label passLabel = new Label("Password:");
   GridPane.setConstraints(grid, 0, 1);      

Adding GridPane to scene:

grid.getChildren().addAll(nameLabel, nameInput, passLabel, passInput, loginButton);
Scene scene = new Scene(grid, 300, 200);

Upvotes: 0

Views: 168

Answers (1)

SedJ601
SedJ601

Reputation: 13858

You are using GridPane.setConstraints(); incorrectly. Instead of setting the constraints on grid, you should be setting the constraints on the nodes. Examples:

// Name label
Label nameLabel = new Label("Username:");
GridPane.setConstraints(nameLabel, 0, 0);     

// Password label
Label passLabel = new Label("Password:");
GridPane.setConstraints(passLabel, 0, 1);   

See javadocs for more details.

Upvotes: 3

Related Questions