1. Computing

Using the BorderLayout


The BorderLayout is the default layout manager for the JFrame. It splits the container into five distinct sections where each section can hold only one component. If you want a certain section to have more than one component then place them into a JPanel and add the JPanel to the the BorderLayout.

Five Component Areas

The five sections of the BorderLayout are known as NORTH, SOUTH, EAST, WEST and CENTER. They are arranged in a similar fashion as the points of a compass. Although it might be easier to think of the BorderLayout as having three rows. The top row is the NORTH section. The bottom row is the SOUTH section. The middle row is made up of three sections - WEST, CENTER and EAST.

In terms of section size, the CENTER section will always try and be the biggest. If a JFrame is made bigger or smaller typically it's the CENTER section that will enlarge or shrink first. The other sections will hold station around the CENTER section.

To add a component to a BorderLayout you just need to specify which section you want it placed:

JFrame guiFrame= new JFrame();
JPanel optionPanel = new JPanel();
guiFrame.add(optionPanel, BorderLayout.NORTH);

Remember that the JFrame uses the BorderLayout by default so the layout manager does not have to be explicitly set to use the BorderLayout layout manager. The BorderLayout field values determine the section to be used:

  • CENTER - middle of the container.
  • EAST - the right side of the middle.
  • WEST - the left side of the middle.
  • NORTH - the top of the container.
  • SOUTH - the bottom of the container.
  • PAGE_START - the equivalent to NORTH for top-down, left-to-right component orientation.
  • PAGE_END - the equivalent to SOUTH for top-down, left-to-right component orientation.
  • LINE_START - the equivalent to WEST for top-down, left-to-right component orientation.
  • LINE-END - the equivalent to EAST for top-down, left-to-right component orientation.

If a section is not specified then the CENTER section will be used. Remember that if a component is already in a section it will be displaced if another component is added to that section.

Note: The BorderLayout field values are strings. This means you can use String literals to set the sections:

guiFrame.add(optionPanel, "North");

However, it's not recommended to use a String literal as it can lead to easy mistakes. The compiler we be unable to spot a misspelled String and as always it is case sensitive.

Component Spacing

The component spacing between the different sections can be set using one of the BorderLayout constructors:

BorderLayout bord = new BorderLayout(5,5);

The individual horizontal and vertical spacing can be set using the setHgap and setVgap methods:


These int values for the horizontal and vertical spacing can be returned by using the getHgap and getVgap methods.

For an example program that shows the different sections of the BorderLayout have a look at the BorderLayout example program. The program allows a user to specify the horizontal and vertical spacing before showing five labels, each placed in one of the different sections.

©2014 About.com. All rights reserved.