Java Swing Tutorial - Java Swing BoxLayout








The BoxLayout arranges components in a container either horizontally in one row or vertically in one column.

The following steps shows how to use a BoxLayout.

First, create a container

JPanel hPanel   = new JPanel();

Then, create an object of the BoxLayout class. When creating BoxLayout object, we need to pass the container its constructor. Other than the container we also passin the layout direction box, either horizontal or vertical to its constructor.

The class has four constants: X_AXIS, Y_AXIS, LINE_AXIS, and PAGE_AXIS. The constant X_AXIS creates a horizontal BoxLayout that lays out all components from left to right.

The constant Y_AXIS is used to create a vertical BoxLayout that lays out all components from top to bottom.

The other two constants, LINE_AXIS and PAGE_AXIS use the orientation of the container in laying out the components.

The following code shows how to create a BoxLayout to lay out components from left to right.

BoxLayout  boxLayout  = new BoxLayout(hPanel, BoxLayout.X_AXIS);

After that, set the layout for the container.

hPanel.setLayout(boxLayout);

Finally, add the components to the container.

hPanel.add(new JButton("Button  1"));
hPanel.add(new JButton("Button  2"));

The full source code.

import java.awt.BorderLayout;
import java.awt.Container;
/*from  w w  w.  j av a2  s  .  c  o m*/
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Main {
  public static void main(String[] args) {
    JFrame frame = new JFrame("BoxLayout  Test");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Container contentPane = frame.getContentPane();

    JPanel hPanel = new JPanel();
    BoxLayout boxLayout = new BoxLayout(hPanel, BoxLayout.X_AXIS);
    hPanel.setLayout(boxLayout);

    for (int i = 1; i <= 5; i++) {
      hPanel.add(new JButton("Button  " + i));
    }

    contentPane.add(hPanel, BorderLayout.SOUTH);
    frame.pack();
    frame.setVisible(true);
  }
}

A BoxLayout tries to give the preferred width to all components in a horizontal layout and the preferred height in a vertical layout.

In a horizontal layout, the height of the tallest component is given to all other components.

In a vertical layout, BoxLayout tries to give the preferred height to all components, and tries to make the size of all components the same width as the widest component.

We can change default alignment by changing either the component's alignment or the container's alignment using the setAlignmentX() method or setAlignmentY() method.





Box

The javax.swing package contains a Box class that makes using a BoxLayout easier.

A Box is a container that uses a BoxLayout as its layout manager.

The Box class provides static methods, createHorizontalBox() and createVerticalBox(), to create a container with a horizontal or vertical layout.

The following code shows how to use Box container.

// Create a  horizontal box
Box hBox = Box.createHorizontalBox();

// Create a  vertical box
Box vBox = Box.createVerticalBox();

To add a component to a Box, use its add() method

hBox.add(new JButton("Button 1"); 
hBox.add(new JButton("Button 2");

The Box class allows us to create invisible components to adjust spacing between two components. It provides four types of invisible components:

  • Glue
  • Strut
  • Rigid Area
  • Filler

A glue is an invisible, expandable component. We can create horizontal and vertical glues using the createHorizontalGlue() and createVerticalGlue() static methods of the Box class.

The following code uses horizontal glue between two buttons in a horizontal box layout.

Box hBox = Box.createHorizontalBox(); 
hBox.add(new  JButton("First")); 
hBox.add(Box.createHorizontalGlue()); 
hBox.add(new  JButton("Last"));

We can also create a glue component using the createGlue() static method of the Box class that can expand horizontally as well as vertically.

A strut is an invisible component of a fixed width or a fixed height.

createHorizontalStrut() method creates a horizontal strut that takes the width in pixels as an argument. createVerticalStrut() method creates a vertical strut that takes the height in pixels as an argument.

The following code shows how to add a 100px strut to a horizontal box

hBox.add(Box.createHorizontalStrut(100));

A rigid area is an invisible component whose size stays the same.

createRigidArea() static method of the Box class, which needs a Dimension object to it to specify its width and height, creates a rigid area.

The following code adds a 10 x 5 rigid area to a horizontal box.

hBox.add(Box.createRigidArea(new  Dimesnion(10, 5)));

A filler is an invisible custom component that we can create by specifying your own minimum, maximum, and preferred sizes.

The Filler static nested class of the Box class represents a filler.

The following code creates a filler, which acts like a glue. The glue is just a filler with a minimum and preferred size set to zero and a maximum size set to Short.MAX_VALUE in both directions.

Dimension  minSize  = new Dimension(0, 0); 
Dimension  prefSize = new Dimension(0, 0);
Dimension  maxSize  = new Dimension(Short.MAX_VALUE,   Short.MAX_VALUE); 
Box.Filler filler = new Box.Filler(minSize, prefSize,  maxSize);

The following code creates a rigid area of 10x10:

Dimension  d  = new Dimension(10, 10);
JComponent rigidArea = new Box.Filler(d, d, d);

Full source code.

import java.awt.BorderLayout;
import java.awt.Container;

import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JFrame;

public class Main {
  public static void main(String[] args) {
    JFrame frame = new JFrame("BoxLayout with Glue");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    Container contentPane = frame.getContentPane();
    Box hBox = Box.createHorizontalBox();
    hBox.add(new JButton("First"));
    hBox.add(new JButton("Previous"));
    hBox.add(Box.createHorizontalGlue());
    hBox.add(new JButton("Next"));
    hBox.add(new JButton("Last"));

    contentPane.add(hBox, BorderLayout.SOUTH);
    frame.pack();
    frame.setVisible(true);
  }
}