Border Layouts


Introduction.  So far, in this course, we have studied FlowLayouts and GridLayouts.  They are probably the two easiest to use.  Now we will present a few other layouts in the next few webpages.  But first I want to list a number of the type of layouts that can be used to arrange components in a GUI.
  • Border Layout
  • Box Layouts
  • Card Layout
  • Flow Layout
  • Grid Layout
  • GridBag Layout

We have already worked with flow and grid layouts.  We will extend this slightly to learn about border and gridbag layouts. 

You can also work with different types of panes in the windows. 

  • Content Panes
  • Desktop Panes
  • Layered Panes
  • Option Panes
  • Scroll Panes
  • Split Panes
  • Tabbed Panes

We have already seen content panes and option panes.  We will extend this slightly to present tabbed panes.

Closing Windows.  Since the first example we will develop involves working with a GUI through an application we need to develop a class to handle the windowClosing event.  Also, we will want to make use of this code elsewhere in the course.

The following is


import java.awt.event.*;

public class ExitWindow extends WindowAdapter

public void windowClosing(WindowEvent e)





When this program is compiled in a directory it will create a class file called ExitWindow.class that will handle a user clicking on the close button if it is referenced in the calling program.  Remember, the compiler needs a way to find this class when working with it in different directories.

Border Layouts.  Now we present a very little bit about border layouts.  They are developed in a GUI by having a north, south, east and west sector around a central region.  The two main methods you need to work with these are given below.  First we see the constructor.


Constructor Description
BorderLayout( ) The constructor is used with no arguments


Since there are five sectors on the layout you need to be able to add things appropriately,  The following is a method used to add components based on a particular layout.


Method Description
add(strSectorIdentifier, component ) This is a typical method to add a component to the layout.  But the first argument takes on one of the following values depending on where the component should be added.
  • "North"
  • "South"
  • "East"
  • "West"
  • "Center"

The quotation marks are used in the code iteself.


This program called will illustrate their basic development.


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

class BorderTest extends JFrame

JButton cmdNorth = new JButton("North Section");
JButton cmdSouth = new JButton("South Section");
JButton cmdEast = new JButton("East Section");
JButton cmdWest = new JButton("West Section");
JButton cmdCenter = new JButton("Center Section");

BorderTest( )

super("Border Layout Example");
JPanel overall = new JPanel( );
overall.setLayout(new BorderLayout( ));
// the order they are added doesn't make any difference
// due to locators in the add commands
overall.add("North", cmdNorth);
overall.add("Center", cmdCenter);
overall.add("South", cmdSouth);
overall.add("East", cmdEast);
overall.add("West", cmdWest);

} // end BorderTest constructor

public static void main(String[] arguments)

JFrame viewFrame = new BorderTest( );
ExitWindow exit = new ExitWindow( );
viewFrame.addWindowListener(exit); );

} // end main method

} // ends BorderTest class


When it is compiled and executed you should see a GUI like the following on your computer screen.



You should see how the center section changes if you drag and resize the window.

There is nothing particularly clever about the code.  though you should notice that you don't have to be as careful about the order in which you add the components to the sectors because they are identified by name.