Java Notes

BorderLayout

Five regions. java.awt.BorderLayout divides a container (eg, JPanel) into 5 geographical sections: North, South, East, West, and Center. This is a commonly used layout.

Expand to fill region. Components start at their preferred size, but are expanded as needed to fill the region they are in.

Use subpanel for more than one component in a region. You can add at most one component to each region of a BorderLayout. To put more than one component in a section, put them in a JPanel (with its own layout), then add that panel to the border layout.

Where extra space goes. The size of a region is adjusted depending on what is in it. If there is nothing in an region, its size will be reduced to zero. Components in the North and South cells are stretched horizontally, and those in East and West are stretched vertically to fill all the space. The center is stretched vertically and horizontally as needed, so it is a good place to put graphics or text areas that you want to expand.

Specifying the region. When you add components to a container which uses BorderLayout, specify the target region as the second parameter as, for example, BorderLayout.NORTH.

Resizing

The window on the left was created by the sample program below. On the right it was made larger by dragging on the lower right corner. Extra vertical space is added to EAST, WEST, and CENTER. Extra horizontal space is added to NORTH, SOUTH, and CENTER.

To prevent component resizing, add a component to a JPanel with FlowLayout, and then add that panel to the BorderLayout. This is a common way to prevent resizing. The FlowLayout panel will stretch, but the component in it will not.

BorderLayout Constructors

If you don't want gaps between regions, use the default constructor, but often the appearance is improved by specifying gaps between the regions, and adding a border to the enclosing JPanel.

content.setLayout(new BorderLayout());          // Default is no gaps
content.setLayout(new BorderLayout(hgap, vgap); // Usually looks better.

Where hgap and vgap are the horizontal and vertical distances in pixels between the regions. Typically they are the same value.

Example of Borderlayout with all regions filled

  1 
  2 
  3 
  4 
  5 
  6 
  7 
  8 
  9 
 10 
 11 
 12 
 13 
 14 
 15 
 16 
 17 
 18 
 19 
 20 
 21 
 22 
 23 
 24 
 25 
 26 
 27 
 28 
 29 
 30 
 31 
 32 
 33 
 34 
 35 
 36 
 37 
 38 
 39 
 40 
 41 
 42 
// File   : layouts/borderLayout/BorderTest.java
// Purpose: Demo use of BorderLayout.
// Author : Fred Swartz - 2006-09-24 - Placed in public domain.

import java.awt.*;
import javax.swing.*;

///////////////////////////////////////////////// class BorderTest
class BorderTest extends JFrame {

    //======================================================= main
    public static void main(String[] args) {
        JFrame window = new BorderTest();
        window.setVisible(true);
    }

    //================================================ constructor
    BorderTest() {
        //... Create components (but without listeners)
        JButton north  = new JButton("North");
        JButton east   = new JButton("East");
        JButton south  = new JButton("South");
        JButton west   = new JButton("West");
        JButton center = new JButton("Center");

        //... Create content pane, set layout, add components
        JPanel content = new JPanel();
        content.setLayout(new BorderLayout());

        content.add(north , BorderLayout.NORTH);
        content.add(east  , BorderLayout.EAST);
        content.add(south , BorderLayout.SOUTH);
        content.add(west  , BorderLayout.WEST);
        content.add(center, BorderLayout.CENTER);

        //... Set window characteristics.
        setContentPane(content);
        setTitle("BorderTest");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        pack();
    }
}

Not all regions are required - gaps and borders improve appearance

If nothing has been added to a region, the neighboring regions expand to fill that space.

This window was created with 5 pixel gaps using only the NORTH, WEST, and CENTER regions. It was then resized, which added both vertical and horizontal space to the regions. This was produced with the code below.

  1 
  2 
  3 
  4 
  5 
  6 
  7 
  8 
  9 
 10 

        //... Create content pane, set layout and border.
        JPanel content = new JPanel();
        content.setBorder(new EmptyBorder(6, 6, 6, 6));
        content.setLayout(new BorderLayout(5, 5));
        
        //... Add components.
        content.add(north , BorderLayout.NORTH);
        content.add(west  , BorderLayout.WEST);
        content.add(center, BorderLayout.CENTER);