๐ JavaFX Tutorial: Layouts & Containers
๐ฏ Goal
In this tutorial, you will learn:
- What containers (layouts) are
- How JavaFX arranges UI elements
- The most commonly used layout types
๐ฆ What is a Container?
A container (also called a layout) is a special node that:
- Holds other UI elements (buttons, labels, etc.)
- Controls how they are positioned and sized
๐ Think of it like a box that organizes its children.
๐ง Why Containers Matter
Without layouts, you would have to manually set:
- X and Y position
- Width and height
โ This is hard and not responsive โ Layouts automatically arrange elements for you
โ๏ธ How Layout System Works
JavaFX uses a hierarchical structure (tree):
Stage (window)
โโโ Scene
โโโ Layout (VBox, HBox, etc.)
โโโ Button
โโโ Label
โโโ TextField
๐ก Key Idea
- Each container decides how to arrange its children
- Children do not control their own position
๐ Common Layout Containers
1๏ธโฃ VBox (Vertical Box)
Arranges elements vertically (top โ bottom)
<VBox spacing="10" alignment="CENTER">
<Button text="Button 1"/>
<Button text="Button 2"/>
</VBox>
๐ง Behavior
Items stacked vertically
Controlled by:
spacingโ space between elementsalignmentโ position inside container
2๏ธโฃ HBox (Horizontal Box)
Arranges elements horizontally (left โ right)
<HBox spacing="10" alignment="CENTER">
<Button text="Yes"/>
<Button text="No"/>
</HBox>
๐ง Behavior
- Items placed in a row
- Same properties as
VBox
3๏ธโฃ BorderPane
Divides the layout into 5 regions:
Top
Left Center Right
Bottom
<BorderPane>
<top><Label text="Top"/></top>
<center><Button text="Center"/></center>
</BorderPane>
๐ง Behavior
- Each area has a fixed position
- Center expands to fill space
4๏ธโฃ GridPane
Creates a grid (rows & columns)
<GridPane hgap="10" vgap="10">
<Button text="1" GridPane.rowIndex="0" GridPane.columnIndex="0"/>
<Button text="2" GridPane.rowIndex="0" GridPane.columnIndex="1"/>
</GridPane>
๐ง Behavior
- Like a table
- You control position using row/column index
5๏ธโฃ StackPane
Stacks elements on top of each other
<StackPane>
<Label text="Background"/>
<Button text="On Top"/>
</StackPane>
๐ง Behavior
- Last element is on top
- Useful for overlays
๐ Important Layout Properties
๐น Spacing
Controls space between children
<VBox spacing="15">
๐น Padding
Controls space inside the container
<VBox>
<padding>
<Insets top="10" right="10" bottom="10" left="10"/>
</padding>
</VBox>
๐น Alignment
Controls position of children
<VBox alignment="CENTER">
Common values:
CENTERTOP_LEFTBOTTOM_RIGHT
๐ Nested Layouts (Very Important)
You can combine layouts:
<VBox spacing="10">
<Label text="Login"/>
<HBox spacing="10">
<Label text="Username:"/>
<TextField/>
</HBox>
<Button text="Submit"/>
</VBox>
๐ก Idea
- Use multiple containers to build complex UI
- Each layout handles a small part
โ ๏ธ Common Mistakes
- โ Using wrong layout type (VBox instead of HBox)
- โ Forgetting spacing โ UI looks crowded
- โ Not using nested layouts
- โ Trying to manually position elements
๐งช Summary
- Containers = layout managers
- They control position + size
- JavaFX UI = tree of containers + nodes
- Use the right layout for the job