Using a BoxLayout Manager : BoxLayout « Swing « Java Tutorial

Arranges components either in a row or in a column.

  1. Box class offers a container that uses BoxLayout as its default layout manager.
  2. BoxLayout works to honor each component's x and y alignment properties as well as its maximum size.

The BoxLayout class has only one constructor:

public BoxLayout(java.awt.Container target, int axis)
  1. 'target' argument specifies the container that needs to be laid out.
  2. 'axis' specifies the axis to lay out components along.

The value of axis can be one of the following:

  1. BoxLayout.X_AXIS
  2. BoxLayout.Y_AXIS
  3. BoxLayout.LINE_AXIS
  4. BoxLayout.PAGE_AXIS
Using a BoxLayout Manager
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;

public class BoxLayoutTest {

  public static void main(String[] args) {
    JFrame frame = new JFrame("BoxLayout Test");
    BoxLayout boxLayout = new BoxLayout(frame.getContentPane(), BoxLayout.Y_AXIS); // top to bottom
    frame.add(new JButton("Button 1"));
    frame.add(new JButton("Button 2"));
    frame.add(new JButton("Button 3"));

Using a BoxLayout Manager
import java.awt.BorderLayout;
import java.awt.Container;
import javax.swing.Box;
import javax.swing.ButtonGroup;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JRadioButton;
public class TryBoxLayout {
  public static void main(String[] args) {
    JFrame aWindow = new JFrame("This is a Box Layout");
    aWindow.setBounds(30, 30, 300, 300);
    // Create left column of radio buttons
    Box left = Box.createVerticalBox();
    ButtonGroup radioGroup = new ButtonGroup();
    JRadioButton rbutton;
    radioGroup.add(rbutton = new JRadioButton("Red"));
    radioGroup.add(rbutton = new JRadioButton("Green"));
    radioGroup.add(rbutton = new JRadioButton("Blue"));
    radioGroup.add(rbutton = new JRadioButton("Yellow"));
    Box right = Box.createVerticalBox();
    right.add(new JCheckBox("A"));
    right.add(new JCheckBox("B"));
    right.add(new JCheckBox("C"));
    Box top = Box.createHorizontalBox();
    Container content = aWindow.getContentPane();
    content.setLayout(new BorderLayout());
    content.add(top, BorderLayout.CENTER);

14.87.1.Using a BoxLayout ManagerUsing a BoxLayout Manager
14.87.2.Creating a BoxLayoutCreating a BoxLayout
14.87.3.A Simple BoxLayout TestA Simple BoxLayout Test
14.87.4.Struts and GlueStruts and Glue
14.87.5.Y-Axis AlignmentY-Axis Alignment
14.87.6.X-Axis AlignmentX-Axis Alignment
14.87.7.Drawing Borders Within a BoxLayout-Managed ContainerDrawing Borders Within a BoxLayout-Managed Container
14.87.8.Separating Components in a Row or Column
14.87.9.Glue spreads the components as far apart as possible.
14.87.10.Strut spreads the components apart by a fixed distance
14.87.11.Laying Out Components in a Row or Column
14.87.12.A vertical box container arranges the components top-to-bottom aligned in their preferred sizes.
14.87.13.Vertical BoxLayout-managed container
14.87.14.Align your components in horizontal or vertical layout
14.87.15.BoxLayout: set a rigid area among our components.