Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Center an object in BorderPane

Tags:

java

javafx

In JavaFX, how to center an object in the top or bottom compartment in BorderPane layout?

I have:

borderPane.setBottom(hBox);

And hBox appears on the left side of the bottom compartment of BorderPane borderPane.And I want it to be in the center of the bottom. Thanks.

like image 509
Dylan Czenski Avatar asked Nov 18 '15 06:11

Dylan Czenski


2 Answers

The default alignment for borderpane positions are:

top: Pos.TOP_LEFT
bottom: Pos.BOTTOM_LEFT
left: Pos.TOP_LEFT
right: Pos.TOP_RIGHT
center: Pos.CENTER

To change this for different alignment use:

BorderPane.setAlignment(child, Pos.CENTER);
BorderPane.setMargin(child, new Insets(12,12,12,12)); // optional
borderPane.setBottom(child);

You may also change the child HBox alignment as:

hBox.setAlignment(Pos.CENTER);

Refer to java API documentation for more info.

like image 153
Uluk Biy Avatar answered Sep 19 '22 04:09

Uluk Biy


You can use the Alignment method like

borderPane.setBottom(hBox);
borderPane.setAlignment(hBox,Pos.CENTER);
like image 45
Collins Abitekaniza Avatar answered Sep 23 '22 04:09

Collins Abitekaniza