Java Swing Tutorials - Herong's Tutorial Examples
Dr. Herong Yang, Version 4.00

java.awt.BoxLayout - Box Layout

This section provides a tutorial example on how to create a BoxLayout to layout components in a container. BoxLayout can have many elements arranged in one direction only: horizontally or vertically.

java.awt.BoxLayout - A layout that:

  • Takes unlimited number of components.
  • Positions each component next to each other only in one direction, horizontal or vertical.
  • Resizes components that are resizable to fill entire container.
  • Resizes components that are resizable when the container is resized.

To test BoxLayout, I wrote another program to try to display my window with BoxLayout:

/**
 * BoxLayoutTest.java
 * Copyright (c) 2002 by Dr. Herong Yang, http://www.herongyang.com/
 */
import java.awt.*;
import javax.swing.*;
public class BoxLayoutTest {
   public static void main(String[] a) {
      JFrame myFrame = new JFrame("BoxLayout Test");
      myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      Container myPane = myFrame.getContentPane();
      myPane.setLayout(new BoxLayout(myPane,BoxLayout.Y_AXIS));
      myPane.add(getFieldPanel());
      myPane.add(getButtonPanel());
      myFrame.pack();
      myFrame.setVisible(true);
   }
   private static JPanel getFieldPanel() {
      JPanel p = new JPanel();
      p.setLayout(new BoxLayout(p,BoxLayout.X_AXIS));
      p.setBorder(BorderFactory.createTitledBorder("Details"));
      p.add(getLabelPanel());
      p.add(getValuePanel());
      return p;
   }
   private static JPanel getButtonPanel() {
      JPanel p = new JPanel();
      p.setLayout(new BoxLayout(p,BoxLayout.X_AXIS));
      p.add(new JButton("OK"));
      p.add(new JButton("Cancel"));
      return p;
   }
   private static JPanel getLabelPanel() {
      JPanel p = new JPanel();
      p.setLayout(new BoxLayout(p,BoxLayout.Y_AXIS));
      JLabel l = new JLabel("Name:");
      l.setAlignmentX(Component.RIGHT_ALIGNMENT);
      p.add(l);
      l = new JLabel("System:");
      l.setAlignmentX(Component.RIGHT_ALIGNMENT);
      p.add(l);
      l = new JLabel("Language:");
      l.setAlignmentX(Component.RIGHT_ALIGNMENT);
      p.add(l);
      l = new JLabel("Year:");
      l.setAlignmentX(Component.RIGHT_ALIGNMENT);
      p.add(l);
      return p;
   }
   private static JPanel getValuePanel() {
      JPanel p = new JPanel();
      p.setLayout(new BoxLayout(p,BoxLayout.Y_AXIS));
      JComponent c = new JTextField(16);
      c.setAlignmentX(Component.LEFT_ALIGNMENT);
      p.add(c);
      JPanel s = getSystemPanel();
      s.setAlignmentX(Component.LEFT_ALIGNMENT);
      p.add(s);
      s = getLanguagePanel();
      s.setAlignmentX(Component.LEFT_ALIGNMENT);
      p.add(s);
      JComboBox b = new JComboBox(
         new String[] {"2001","2002","2003"});
      b.setAlignmentX(Component.LEFT_ALIGNMENT);
      p.add(b);
      return p;
   }
   private static JPanel getSystemPanel() {
      JRadioButton unixButton = new JRadioButton("Unix",true);
      JRadioButton winButton = new JRadioButton("Window",false);
      ButtonGroup systemGroup = new ButtonGroup();
      systemGroup.add(unixButton);
      systemGroup.add(winButton);
      JPanel p = new JPanel();
      p.setLayout(new BoxLayout(p,BoxLayout.X_AXIS));
      p.add(unixButton);
      p.add(winButton);
      return p;
   }
   private static JPanel getLanguagePanel() {
      JPanel p = new JPanel();
      p.setLayout(new BoxLayout(p,BoxLayout.X_AXIS));
      p.add(new JCheckBox("Java",true));
      p.add(new JCheckBox("C++",true));
      p.add(new JCheckBox("Perl",false));
      return p;
   }
}

Run it. What do you think about the result? Much better, right?
Layout - BoxLayout

But if you look closely, value components are not aligned to the corresponding label components. It is almost impossible to align them, because they are in two different panels.

So, BoxLayout is still not good for my example.

Another question about BoxLayout is why the constructor needs to take the container as input. Constructors of other layouts do not need containers. This makes the statement looks very strange: p.setLayout(new BoxLayout(p,...)).

Sample programs listed in this section have been tested with JDK 1.3.1, 1.4.1, 1.5.0, and 1.6.0.

Last update: 2009.

Sections in This Chapter

What Is Layout?

java.awt.BorderLayout - Border Layout

java.awt.FlowLayout - Flow Layout

java.awt.BoxLayout - Box Layout

java.awt.GridLayout - Grid Layout

java.awt.GridBagLayout - Grid Bag Layout

Dr. Herong Yang, updated in 2009
java.awt.BoxLayout - Box Layout