JavaFX: Fit child elements to HBox width
Asked Answered
I

4

8

Is it possible to manage child elements in a HBox, so that the sum of the widths of all child elements is equal to the width of the HBox?

So that elements fill the HBox and no space is left.

The height of the child elements is by default the height of the HBox, so how about the width? I don't want to calculate the width in my program. It would be better if the layout does this automatically, so that no calculation is needed.

Inheritance answered 26/2, 2014 at 14:31 Comment(5)
HBox lays out its children horizontally. It does not make sense to make every child the same width as the HBox.Proustite
No, not the same width as the hbox, the width summary of all childs should be equal to the hbox width. So that the hbox is filled fully with the childs and no space is left.Inheritance
Should the children have equal width?Proustite
yes, all the same widthInheritance
@Inheritance just dont specify the HBox's width and it will occupy the width of all the children !Flocculant
T
16

It depends what kind of children does the HBox contain. Some of the children may not be resizable nodes. However, generally speaking, you can use HBox.setHgrow() method and set the same Priority for all children of hbox. The explanation is in its javadoc:

Sets the horizontal grow priority for the child when contained by an hbox. If set, the hbox will use the priority to allocate additional space if the hbox is resized larger than it's preferred width. If multiple hbox children have the same horizontal grow priority, then the extra space will be split evening between them. If no horizontal grow priority is set on a child, the hbox will never allocate it additional horizontal space if available. Setting the value to null will remove the constraint.

Additionally, if you are trying to obtain a grid-like layout then try out other layout options, for instance TilePane or FlowPane and maybe GridPane.

Turino answered 27/2, 2014 at 6:33 Comment(1)
setHgrow has solved the problem. Other layouts like gridpane was no good option for me, because the child nodes in the hbox (flowpanes) should be resizable. With gridpane i had lots of trouble with the resizing of the child nodes.Inheritance
B
16

The simple code for your answer would be this:

Button button1 = new Button("Save");
Button button2 = new Button("Delete");
HBox.setHgrow(button1, Priority.ALWAYS);
HBox.setHgrow(button2, Priority.ALWAYS);

It's recommended to use HBox class rather than name of HBox pane.

Besprinkle answered 22/2, 2017 at 16:6 Comment(0)
E
9

In FXML:

<HBox>
   <Label HBox.hgrow="ALWAYS" maxWidth="Infinity">
       TEST
   </Label>
</HBox>
Epinephrine answered 2/11, 2017 at 11:47 Comment(0)
Z
1

Adding to @bdshahab's answer, since HBox will try to resize their children to their preferred widths, they will only fill the box if their combined preferred widths are wider than the HBox width.

So it may be necessary to do something like:

Button button1 = new Button("Save");
Button button2 = new Button("Delete");
HBox.setHgrow(button1, Priority.ALWAYS);
HBox.setHgrow(button2, Priority.ALWAYS);
button1.setPrefWidth(100000);
button2.setPrefWidth(100000);
Zest answered 10/9, 2017 at 23:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.