Allowing a label to grow beyond its preferred size
The label is bounded by its maxWidth
which defaults to its prefWidth
.
You want to make the maxWidth
unbounded, so call:
label.setMaxWidth(Integer.MAX_VALUE)
Different controls have different defaults for maxWidth
. For buttons and labels the maxWidth
default to their prefWidth
. Unless you reset the maxWidth
these controls won't grow; even if you provide hints that you might think would make them do so.
Quoting the Oracle JavaFX layout sizing tutorial:
UI controls also provide default minimum and maximum sizes that are based on the typical usage of the control. For example, the maximum size of a Button
object defaults to its preferred size because you don't usually want buttons to grow arbitrarily large. However, the maximum size of a ScrollPane
object is unbounded because typically you do want them to grow to fill their spaces.
It would be nice if this detail of information was also in java doc of each control, so you would know which controls default their max size to the pref size. There is some information on this in the HBox
java doc.
Spring alternative
An alternative is to insert a spring pane into the hbox before the label and set the hgrow on the spring pane instead. That will push the label all the way to the right. For info on that approach see:
Alternative layout
As noted by DaveB in the comments:
Perhaps, if you simply want the Label over to the right, using AnchorPane instead of HBox would be better. Just anchor the Label to the right side. Choosing the correct layout container can often be the key to retaining your sanity.
Example FXML
I don't write Kotlin, so I will provide a layout example with FXML instead and you can translate it to Kotlin if you wish.
The example sets the maxWidth
of a label to a large number so that the label can grow to fill the available area, with the text in the label right aligned.

<HBox prefHeight="50.0" prefWidth="150.0" xmlns="http://javafx.com/javafx/18" xmlns:fx="http://javafx.com/fxml/1">
<children>
<Label alignment="CENTER_RIGHT" maxWidth="1.7976931348623157E308" text="Label" HBox.hgrow="ALWAYS" />
</children>
</HBox>
FAQ
That worked. But I can see that's going to be hard to remember next time.
Just my opinion, others have different opinions:
- It will probably be easier to remember than you may think. Once something like this bites you, it is difficult to forget.
- If something is not resizing how you expect, just try setting the max width (and/or max height).
- Use scene builder for prototyping such things, removing any default sizing it sets for layouts and providing sizing hints only where needed.
- Scene Builder is quicker and easier for layout than writing things in code (for many, but not all tasks), plus the result is easier to maintain.
Do you happen to know why label.prefWidth=Double.MAX_VALUE did not work?
It only "produced" a "..." text instead of "Label" and it remained on the right side of the application window.
It doesn't make a lot of sense to prefer something to be an infinite size, you could never see it at the preferred size.
It probably confused the layout algorithm. I'm guessing it sized the label to the preferred size, then needed to round it up to snap it for pixel alignment or add to the margin. This would cause a double overflow, making the preferred size negative, which doesn't make sense. So it would try to size to the minimum size instead. When there is not enough space to display a value, a label will elide the text (replacing characters with ...
), which is what it did here. The default minimum size will be just enough to display the elided text.
If you also set minSize
to 100 and the prefSize
to Double.MAX_VALUE
, the label will show all of the text aligned within the minSize, which indicates that it is the minimum size that is being respected in such a case.
Anyway, don't set the prefWidth
to Double.MAX_VALUE
. Instead, set the maxWidth
to some large number so that the field can grow to fit it (e.g. Integer.MAX_VALUE
).