yemerra
yemerra

Reputation: 1342

JavaFX: HBox inside a StackPane not in the center

I used StackPane where all nodes are in the center. Now I want to have a node that is a HBox so that I can have horizontal elements in the center. For some reason, the HBox is at the top left even though its a node of StackPane.

HBox lines = new HBox();
lines.setSpacing(20);
lines.getChildren().addAll(firstLine, secondLine, thirdLine, fourthLine);

Here is the part with the HBox, and here where I add the HBox:

StackPane root = new StackPane();
root.getChildren().add(lines);

What am I missing?

Upvotes: 0

Views: 2638

Answers (1)

Uluk Biy
Uluk Biy

Reputation: 49185

The HBox is at the center of StackPane, more accurately it will fill all available space of StackPane. The items you see on top left are due to the HBox is aligning them like that way by default. Change this default value to

lines.setAlignment( Pos.CENTER );

Upvotes: 3

Related Questions