Use BoxLayout
You can use the visual editor to layout Swing components using BoxLayout on the X or Y axis.
BoxLayout is a Swing layout manager that allows multiple components to be laid out either vertically or horizontally. The components will not wrap, so, for example, a vertical arrangement of components will stay vertically arranged when the frame is resized.
For all directions, components are arranged in the same order as they were added to the container. BoxLayout attempts to arrange components at their preferred widths (for horizontal layout) or heights (for vertical layout). For a horizontal layout, if not all the components are the same height, BoxLayout attempts to make all the components as high as the highest component. If that's not possible for a particular component, then BoxLayout aligns that component vertically, according to the component's Y alignment. By default, a component has a Y alignment of 0.5, which means that the vertical center of the component should have the same Y coordinate as the vertical centers of other components with 0.5 Y alignment.
Similarly, for a vertical layout, BoxLayout attempts to make all components in the column as wide as the widest component. If that fails, it aligns them horizontally according to their X alignments.
To work with BoxLayout:
- Select a container in the Design view or Java Beans view.
- In the Properties view, set the layout property to one of the following options:
- BoxLayout(X_AXIS) - Components are laid out horizontally from left to right.
- BoxLayout(Y_AXIS) - Components are laid out vertically from top to bottom.
- Drop components from the palette onto the BoxLayout. A black bar next to the cursor will indicate the placement of a component that is about to be added to the BoxLayout.
Parent topic
Swing and AWT layout managers
Related concepts
Layout managers and containers
Related tasks
Using BorderLayout
Using CardLayout
Using FlowLayout
Using GridLayout (AWT)
Using GridBag layout