Java Swing Layouts

Summary: Java Swing provides you with various layouts to allow you to arrange Swing components within a container. It is very important to understand layout manager before you dive into different kinds of layouts.

A layout manager is an implementation of LayoutManager interface. A layout manager object determines the position and size of a Swing component inside a container. There are implementations of LayoutMager interface such as GridLayoutFlowLayout, and ScrollPaneLayout.

Since JDK 1.1, LayoutManager2 interface was introduced. The LayoutManager2 extends LayoutManager to provide more functionalities. The implementations of LayoutManager2 can be named such as CardLayoutGridBagLayout, BoxLayout, and BorderLayout.

There are several main tasks related to the layout manager such as:

  • Setting the layout manager to a container.
  • Adding a Swing component to a container based on its layout manager.
  • Providing alignment and size hints.
  • Adding spaces between Swing components inside a container.
  • Setting orientation for the container.

Setting the layout manager to a container

By default, JPanel uses FlowLayout and content pane uses BorderLayout when those containers are initialized. If you want to have different layout manager, you can change it when creating new container objects or using setLayout() method when the object had been already created.

To set BoxLayout to a JPanel you can use the following code snippet:

JPanel panel = new JPanel(new BoxLayout());Code language: JavaScript (javascript)

After creating panel is created, you can change the layout manager by using setLayout() method as follows:

panel.setLayout(new GridLayout());Code language: JavaScript (javascript)

If you set the LayoutManager object of a container to null, you are telling the swing framework that you use absolute positioning. In this case, you must specify position and size for each every component within that container. The limitation of this method is that absolute positioning does not adjust accurately when the top-level container resized.

Adding a Swing component to a container using LayoutManager

It depends on the LayoutManager object you use when you add a Swing component to a container. Different layout manager requires different parameter in the add() method of a container. Some LayoutManager objects even do not require a parameter for instance GroupLayout.  For example, if we use BorderLayout we need to set a parameter in add() method of a component as follows:

JButton btnOK = new JButton("OK");
JButton btnCancel = new JButton("Cancel");
JPanel panel = new JPanel(new BorderLayout());
panel.add(btnOK,BorderLayout.LINE_START);
panel.add(btnCancel,BorderLayout.LINE_END);Code language: JavaScript (javascript)

In the example above, we created two new buttons and a JPanel with BorderLayout. We added the OK button at the start of the line and Cancel button at the end of the line. Here is the screenshot of the layout producing by the code snippet above.

BorderLayout

Providing alignment and size hints

In order to allow the container to laid out the components inside it accurately, you need to provide the alignment and size hints using a list of Swing component methods. Those methods are mostly dealing with minimum, maximum, preferred size, vertical and horizontal alignment of a component.

MethodsDescription
 public void setMaximumSize(Dimension maximumSize) Set maximum size of the component
 public void setMinimumSize(Dimension minimumSize) Set minimum size of the component
 public void setPreferredSize(Dimension preferredSize) Set the preferred size of the component.
 public void setAlignmentX(float alignmentX) Set the vertical alignment of the component
 public void setAlignmentY(float alignmentY) Set the horizontal alignment of the component

Adding space between components inside a container

There are several ways you can use to add spaces between components inside a container. It is important to understand those methods in order to manipulate space between component effectively.

  • Layout Manager: it depends on layout manager in term of manipulating space between components. Some Layout managers allow you to add space, others do not. Some Layout managers also automatically add space between components for you, so refer to each individual Layout Manager to see how it works.
  • Border:  by adding an empty border to change the amount of space between components regardless of Layout Manager.
  • Lightweight invisible component: you can adding space between components by creating the lightweight invisible component. This component will take up space in a container, therefore, add more space between each component.

Setting orientation for the container

Different languages have different orientations. In English, we have left to right and top to bottom orientation. It means components are laid out inside a container from left to right and from top to bottom. In other languages such as Arabic, the component should be laid out from right to left.

To set component’s orientation, you can use either of these methods: setComponentOrientation() or applyComponentOrientation(). You can get the orientation of a container based on a given locale by using method getOrientation() before setting orientation. Here is the code snippet to set the orientation of the Arabic language:

JFrame frame = new JFrame("Component Orientation Demo");
JComponent.setDefaultLocale(new Locale("ar"));
Container contentPane = frame.getContentPane();
ComponentOrientation orientation = ComponentOrientation.getOrientation(
                        contentPane.getLocale());
contentPane.applyComponentOrientation(orientation);Code language: JavaScript (javascript)