Layout Panes are containers that automatically arrange the Controls (and any Nodes) you add to them using a set of rules. Each Layout Pane has its own rules.
Oracle tutorial:
Usage Tips:
This list is a quick summary of the built-in JavaFX Layout Panes
Layout Pane | Description |
---|---|
BorderPane | Has top, bottom, left, right, and center regions. Each region can contain only one Node, but it can be another Layout Pane in which you put more content. You cannot use .getChildren().add(content) with this container. See the tutorial above for the correct way to add content. |
HBox | Contents are arranged horizontally |
VBox | Contents are arranged vertically |
StackPane | Contents are centered and stacked on top of each other |
GridPane | Contents are placed in a n x m grid, where rows/cols can be different sizes and cells can span multiple rows/cols |
FlowPane | Contents wrap just like text wrapping |
TilePane | Contents are placed in a n x m grid, where each cell is the same size |
AnchorPane | Contents are anchored to specific regions of the AnchorPane |
Useful methods common to all Layout Panes:
Methods | Description |
---|---|
setAlignment() | Sets the alignment of Nodes within this Layout Pane |
setPadding() | Puts padding between the Border and internal contents |
setBorder() | Sets the Border style |
setPreferredWidth() setPreferredHeight() | Tells your parent Node your preferred size. Ignored if parent Node has layout rules that override your request. |
setMinWidth(), setMaxWidth() setMinHeight(), setMaxWidth() | Prevents this Pane from being bigger/smaller than this when resized. Ignored if parent Node has layout rules that override your request. |
setLayoutX(), setLayoutY() | Hardcodes the position of a layout pane, but only works if your parent doesn't have layout rules (eg: Group) |
Read the API for additional methods for specific Layout Panes.
For example, HBox has these specific methods:
Methods | Description |
---|---|
setSpacing() | Puts spacing between the internal contents |
setHgrow() | Used to decide how each internal item takes up extra space. For example, if you had two Buttons in an HBox, you could tell one button to always grow to fit the empty space and tell the other button to never grow. |
Watch this demo to see how to use HGrow to control widths in an HBox as the scene is resized.
Last modified: February 28, 2023
Back to Control Nodes