Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to adjust Label's width to fit its content?

Tags:

java

label

javafx

How one can adjust minimum width of the JavaFX 8 (8_45) Label control item according to its content? To this very moment I had to adjust size of my GUI components manually, in order to be sure, that their content will be visible no matter what will happen with the size of its parent (eg. HBox, Scene, Stage or whatever), ie.:

Label label = new Label("Foo foo foo");
label.setMinWidth(someMinValue);

Is there a way to make Label or any other JavaFX control item to "listen" its content and adjust its width to it automatically? Thank you in advance.

like image 784
bluevoxel Avatar asked May 25 '15 13:05

bluevoxel


1 Answers

If you want to make sure your label stays big enough for the text it holds, you probably want to set its minimum size to track its preferred size:

label.setMinWidth(Region.USE_PREF_SIZE);
like image 185
Steven Van Impe Avatar answered Nov 08 '22 22:11

Steven Van Impe