Java Swing BoxLayout example

1. Introduction

In this article, we’ll discuss about Java Swing BoxLayout. In Java Swing, layout manager is in charge of laying out different components in specific positions. In Java Swing and awt package, several layout managers are popularly used, such as BorderLayout, BoxLayout, CardLayout, FlowLayout and GridLayout. In this article, we’ll focus on BoxLayout and go through the basic operation on it.

In BoxLayout, it puts components in a single row or a single column. Only one constructor is used by BoxLayout: BoxLayout(Container target, int axis), which will lay out components along the given axis. For the container target, it can be a frame, a panel or other containers to hold the components. For the axis, four different axes could be chosen: X_AXIS, Y_AXIS, LINE_AXIS, PAGE_AXIS. As X_AXIS and Y_AXIS are more often used, we’ll show only examples with these two axes. If you’re interested in the other two, you can refer to the official doc from Oracle here. Also, the alignment settings of different components will also have influence on the layout of the whole GUI.

2. Swing BoxLayout example with different axis parameters

In this example, we’ll compare the results of BoxLayout with two different axis options: X_AXIS and Y_AXIS. In X_AXIS, components are laid out horizontally from left to right, while in Y_AXIS vertically from top to bottom.

To compare the difference layout between these two settings, a JFrame needs to be created as the largest container. Then a JPanel is added on this frame and we can set the accurate size of it. After this operation, three buttons (Button1, Button2, Button3) are generated and added in the panel. The layout can be defined when the the the panel is added.

Below it the Java code for this example:


package javaCodeGeeks;

 * A swing BoxLayout example with different Boxlayout axis parameters
 * BoxLayout.X_AXIS, BoxLayout.Y_AXIS respectively.
 * Also add spaces between components.

import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.BoxLayout;
import javax.swing.Box;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import java.awt.Insets;
import java.awt.Dimension;

public class swingBoxLayoutAxis {

	public static void main(String[] args) {
		// Create and set up a frame window
		JFrame frame = new JFrame("BoxLayout Example X_AXIS");
		// Set the panel to add buttons
		JPanel panel = new JPanel();
		// Set the BoxLayout to be X_AXIS: from left to right
		BoxLayout boxlayout = new BoxLayout(panel, BoxLayout.Y_AXIS);
		// Set the Boxayout to be Y_AXIS from top to down
		//BoxLayout boxlayout = new BoxLayout(panel, BoxLayout.Y_AXIS);

		// Set border for the panel
		panel.setBorder(new EmptyBorder(new Insets(150, 200, 150, 200)));
		//panel.setBorder(new EmptyBorder(new Insets(50, 80, 50, 80)));		
		// Define new buttons
		JButton jb1 = new JButton("Button 1");
		JButton jb2 = new JButton("Button 2");
		JButton jb3 = new JButton("Button 3");
		// Add buttons to the frame (and spaces between buttons)
		//panel.add(Box.createRigidArea(new Dimension(0, 60)));		
		//panel.add(Box.createRigidArea(new Dimension(0, 60)));
		// Set size for the frame
		//frame.setSize(300, 300);
		// Set the window to be visible as the default to be false



BoxLayout Example on XAXIS
BoxLayout Example on XAXIS

The code above shows the scenario when we use the X_AXIS layout. Run this code in Eclipse and we’ll get the following figure below. From this figure we can see that the three buttons are close to each other and laid out in a horizontal way from left to right.

BoxLayout Example on YAXIS
BoxLayout Example on YAXIS

Then we comment the line 31 and uncomment the line 34 to get the layout of Y_AXIS. As we can see the figure below, the three buttons changes from the previous horizontal way to be vertical.

3. Swing BoxLayout example using invisible components as filler

In Java Swing, if you want to have spaces between components, two options could be considered: 1. add empty space to one or more components; 2. insert invisible components to obtain spaces between components. Here Box.Filler class, which is defined in Box class, would be helpful to achieve the second option. Four different methods can be chose to achieve this:

  • Rigid area, use Box.createRigidArea(size)
  • Glue horizontal, use Box.createHorizontalGlue()
  • Glue vertical, use Box.createVerticalGlue()
  • Custom Box.Filler, use new Box.Filler(minSize, preSize, maxSize)

In createRigidArea(size) method, it creates an invisible component with a specified size. For the size parameter, it could be defined by new Dimension(int width, int height). For example, if new Dimension(0, 80) is called between two buttons, 80 pixels space is put between this two buttons in a top-to-bottom way. Vise verse, (20, 0) in a left-to-right way. Through this method, rigid invisible area is created. You could use the following code:

container.add(Box.createRigidArea(new Dimension(0, 80)));

BoxLayout Example on Filler
BoxLayout Example on Filler

The following figure shows the output when we add rigid area (0, 25) between the three buttons.

In createHorizontalGlue() and createVerticalGlue() methods, they are very similar to each other. The only difference between these two methods are the orientation of the glue to be put. It won’t specify the rigid size of the invisible space. In createHorizontalGlue() method, after calling it, a horizontal glue will be created between two components in a horizontal (left-to-right) way, while in createVerticalGlue() method a vertical glue will be created in a vertical (top-to-bottom) way. These space generated by these two methods will squeeze the two components to be in the border of the container, and take the maximum space between the two components. You could use the following code:


Box.Filler(minSize, preSize, maxSize) is a custom method to create invisible space. You can specify the minimum, preferred and maximum sizes that you want the space to be. For example, to create a space with 20 pixels in width and 30 pixels in height, you could use the following code. Also, you can defined the minimum size to be 10 by 20 and maximum size to be 50 by 100.

Dimension minSize = new Dimension(10, 20);
Dimension prefSize = new Dimension(20, 30);
Dimension maxSize = new Dimension(50, 100);
container.add(new Box.Filler(minSize, prefSize, maxSize));

4. Swing BoxLayout example with different alignment settings

In Java Swing, we can also control the alignment of different components. The alignment setting could come into play when the size of the component is smaller than the size of the container. Three different alignment settings could be used:

  • Component.LEFT_ALIGNMENT

For LEFT_ALIGNMENT, which has a value of 0.0, the left sides of all components should be aligned. For CENTER_ALIGNMENT, which has a value of 0.5, the center of all components should be aligned. For RIGHT_ALIGNMENT, which has a value of 1.0, the right sides of all components should be aligned.

Below is the swingBoxLayoutAlignment example to show the difference between the three alignment options:


package javaCodeGeeks;

 * A swing BoxLayout example with different BoxLayout alignment settings

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

import java.awt.Component;
import java.awt.FlowLayout;

public class swingBoxLayoutAlignment {

	public static void main(String[] args) {
		// Create and set up a frame window
		JFrame frame = new JFrame("BoxLayout Example Alignment");
		// Define new buttons with different width on help of the ---
		JButton jb1 = new JButton("Button 1 -");		
		JButton jb2 = new JButton("Button 2 --------");
		JButton jb3 = new JButton("Button 3 -");		
		JButton jb4 = new JButton("Button 4 --------");
		JButton jb5 = new JButton("Button 5 -");		
		JButton jb6 = new JButton("Button 6 --------");
		// Define the panel to hold the buttons	
		JPanel panel1 = new JPanel();
		JPanel panel2 = new JPanel();
		JPanel panel3 = new JPanel();
		// Set up the title for different panels
		// Set up the BoxLayout
		BoxLayout layout1 = new BoxLayout(panel1, BoxLayout.Y_AXIS);
		BoxLayout layout2 = new BoxLayout(panel2, BoxLayout.Y_AXIS);
		BoxLayout layout3 = new BoxLayout(panel3, BoxLayout.Y_AXIS);
		// Add the buttons into the panel with three different alignment options
		// Add the three panels into the frame
		frame.setLayout(new FlowLayout());
		// Set the window to be visible as the default to be false


The output for this example is shown below:

BoxLayout Example on Alignment
BoxLayout Example on Alignment

We can see that button1 and button2 are aligned to the left side of the panel, button3 and button4 are in the center of the panel, while button5 and button6 are right side of the panel.

5. Download the Source Code

This was an example of BoxLayout in Java Swing.

You can download the full source code of this example here: swingBoxLayoutExample

Jun Wu

Jun (Steven) Wu is a current Master student in Computer Science & Engineering department of University of Nebraska Lincoln (Lincoln, NE, USA). His current interests focus on Programming Languages (Java, Python), Relational Database (MySQL), NoSQL Database (Apache Cassandra, MongoDB), and Computer Networks.
Notify of

This site uses Akismet to reduce spam. Learn how your comment data is processed.

1 Comment
Newest Most Voted
Inline Feedbacks
View all comments
Jan Egil Kristiansen
4 years ago

In line 31-31:
// Set the BoxLayout to be X_AXIS: from left to right
BoxLayout boxlayout = new BoxLayout(panel, BoxLayout.Y_AXIS);
I believe both axis should be X_AXIS ?

Back to top button