1. Technology

An Overview of Layout Managers


Businessman using laptop in office
Granger Wootz/Blend Images/Getty Images

A graphical user interface uses graphical components (e.g., JTextField, JButton, JLabel) to enable a user to interact with an application. These components need to be placed into a container (e.g., JFrame, JPanel) to build an interface. The way the components are grouped together in the container are very important in making sure the interface is useful to the user. If you've ever used a program where it wasn't very clear how to perform a function you'll know it can be a frustrating experience. Where you place the components in a container can be made easier by choosing the right layout manager.

Layout Managers

A layout manager specifies the position and size of the components inside a container. There are several layout managers provided in the Java API.

The FlowLayout

A JPanel uses the FlowLayout as its default layout manger. It's the simplest layout manager as it places the components in a row. If the row reaches the edge of the container it starts another row and so on.

Have a look at Using the FlowLayout for more specific details on how to use the FlowLayout.

If you run the GoingWithTheFlow program you can see the FlowLayout in action. Every time you press the "Add Button" button a new button appears on the row. If you keep adding buttons the JPanel will be filled with row after row of buttons. The FlowLayoutExample program provides a user with the ability to set alignment and component spacing options for a FlowLayout manager of a JPanel.

The BorderLayout

The BorderLayout splits the container into five different sections. The top and bottom sections are called north and south. They run from the left edge of the container to the right edge. The space in between the top and bottom sections is split into three sections known as west, center and east. If you run the BorderLayoutExample program you'll see how the sections appear on a JFrame

Have a look at Using the BorderLayout for more specific details on how to use the BorderLayout.

The BoxLayout

The BoxLayout puts the components either in one row or column. Unlike the FlowLayout there is no wrapping around to another row or column if the edge of the container is reached. If there are too many components to fit in the row then the ones that don't fit can't be seen.

The GridLayout

The GridLayout places each component into a cell of a grid made up of a specified number of rows and columns (e.g., think of a spreadsheet). Each cell in the grid is exactly the same size and the component is sized to take up the entire cell size.

The GridLayout example program shows how a GridLayout can be used to make a simple calculator graphical user interface. It also allows the user to customize the GridLayout.

The GridBagLayout

The GridBagLayout is similar to the GridLayout in that it uses a grid made up of a number of rows and columns. The difference is that the rows and columns do not have to be the same height or width. Also, a component can span across a number of cells.

The GroupLayout

The GroupLayout can be quite complicated to use as it deals with the horizontal and vertical layouts separately. Therefore each component must be specified in both the horizontal and vertical layouts.

The CardLayout

The CardLayout allows you to make an area which can display different components at different times. Like a stack of playing cards only the top card is seen.

The SpringLayout

The SpringLayout is one of the most flexible layout managers as the position of each component is defined by the distance to the edges of another component. It can emulate most of the other layout managers but the more components you work with the more complex the layout becomes.

  1. About.com
  2. Technology
  3. Java
  4. Create User Interfaces
  5. Layout Managers
  6. An Overview of Layout Managers

©2014 About.com. All rights reserved.