It seems that many answers have already been provided, and they should work. However , there is a way to set percentages:
<fx:define> <Screen fx:factory="getPrimary" fx:id="screen" /> </fx:define>
This will help you determine the size of the current screen on which the application is displayed. Now that we have the dimensions of the display, we can play with it in FXML as follows:
<HBox fx:id="hroot" prefHeight="${screen.visualBounds.height}" prefWidth="${screen.visualBounds.width}"> Your FXML elements inside the root... </HBox>
Please note that I use visualBounds , as this will provide me with available space on the screen, since I do not want to overlap the taskbar in Windows, for example. For full-screen applications, you just use the "borders".
Now, to understand how to use percentages, you can play with the values โโof prefheight and prefWidth. You can do the calculations inside $ {} .
Not necessary:
If you want all your elements to use relative sizes, just refer to them using their identifier, width or height property and make your calculation.
<VBox fx:id="VBSidebar" prefWidth="${hroot.width*0.15}" prefHeight="${hroot.height}"> more elements.. </VBox>
Hope this helps!
Jarrick
source share