Dylan Czenski
Dylan Czenski

Reputation: 1365

How to center all elements from top to bottom in a GridPane

GridPane.setConstraints(button1,0,0);
GridPane.setConstraints(button2,0,1);
GridPane.setConstraints(button3,0,2);

gridPane.getChildren().addAll(button1,button2,button3);

I have some buttons in a GridPane as they are shown above, and I want to

  1. center them all in gridPane;
  2. center each button's label within their button boundary (optional).

Currently the buttons are aligned to the left and they don't look so good. Can someone please tell me what to do? Thanks

Upvotes: 0

Views: 154

Answers (1)

Uluk Biy
Uluk Biy

Reputation: 49185

You can set the vertical and horizontal alignments of each cell by using other constraint options:

GridPane.setConstraints( b1, 0, 0, 1, 1, HPos.CENTER, VPos.CENTER );
GridPane.setConstraints( b2, 0, 1, 1, 1, HPos.CENTER, VPos.CENTER );
GridPane.setConstraints( b3, 0, 2, 1, 1, HPos.CENTER, VPos.CENTER );

You may also set the gaps between the cells with:

gridPane.setVgap( 10 );
gridPane.setHgap( 10 );

to debug those values visually use:

gridPane.setGridLinesVisible( true );

Upvotes: 1

Related Questions