Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set a window minimum size according to its content in JavaFX?

Tags:

java

javafx

To statically set a window minimum size to a 100x100 pixels frame in JavaFX 2.2 one would use:

stage.setMinWidth(100);
stage.setMinHeight(100);

Supposing we don't want to set a fixed minimum size, but we want the window to be resized to a minimum size that all its content (buttons, text fields, etc.) is still fully visible. How would we do that?

like image 963
Marco Lackovic Avatar asked Mar 20 '14 11:03

Marco Lackovic


2 Answers

You can try to use stage.minWidthProperty().bind(... Binding expression here...);

The difficulty of course is to define the binding expression... it should depend on all your components but still it should work.

Resizing children will modify the binding expression and will change the minWidthProperty on your Stage.

like image 73
zenbeni Avatar answered Nov 04 '22 19:11

zenbeni


I suggest you to read the javadoc of Pane, Region and maybe Group nodes. On how they layout and resize their children. Basically if you don't set the preferred size of the pane then, by default, it will be calculated according to its content. Also have a look on Window.sizeToScene() method.

like image 24
Uluk Biy Avatar answered Nov 04 '22 20:11

Uluk Biy