The JavaTM Tutorial
Previous Page Lesson Contents Next Page Start of Tutorial > Start of Trail > Start of Lesson Search
Feedback Form

Trail: Creating a GUI with JFC/Swing
Lesson: Laying Out Components Within a Container

How to Use BorderLayout

Here's an applet that shows a BorderLayout (in the API reference documentation) in action.

Click this figure to run the applet.
This is a picture of the applet's GUI. To run the applet, click the picture. The applet will appear in a new browser window.

As the preceding applet shows, a BorderLayout has five areas: north, south, east, west, and center. If you enlarge the window, the center area gets as much of the available space as possible. The other areas expand only as much as necessary to fill all available space. Often, a container uses only one or two of the areas of the BorderLayout -- just the center, or center and south, for example.

The following code creates the BorderLayout and the components it manages. You can find the whole program in (in a .java source file). The program runs either within an applet (with the help of AppletButton (in a .java source file)) or as an application.

Container contentPane = getContentPane();
//Use the content pane's default BorderLayout.
//contentPane.setLayout(new BorderLayout()); //unnecessary
contentPane.add(new JButton("Button 1 (NORTH)"),
contentPane.add(new JButton("2 (CENTER)"),
contentPane.add(new JButton("Button 3 (WEST)"),
contentPane.add(new JButton("Long-Named Button 4 (SOUTH)"),
contentPane.add(new JButton("Button 5 (EAST)"),

Important:  When adding a component to a container that uses BorderLayout, specify the component's location as one of the arguments to the add method. Do not rely on the component being added to the center, by default. If you find that a component is missing from a container controlled by a BorderLayout, make sure that you specified the component's location and that you didn't put another component in the same location.

All our examples that use BorderLayout specify the component as the first argument to the add method. For example:

add(component, BorderLayout.CENTER)  //preferred
However, you might see code in other programs that specifies the component second. For example, the following are alternate ways of writing the preceding code:
add(BorderLayout.CENTER, component)  //valid but old-fashioned
add("Center", component)             //valid but error prone

The BorderLayout API

By default, a BorderLayout puts no gap between the components it manages. In the preceding applet, any apparent gaps are the result of the buttons reserving extra space around their apparent display area. You can specify gaps (in pixels) using the following constructor:
BorderLayout(int horizontalGap, int verticalGap)
You can also use the following methods to set the horizontal and vertical gaps, respectively:
void setHgap(int)
void setVgap(int)

Examples that Use BorderLayout

The following table lists some of the many examples that use BorderLayout.

Example Where Described Notes
BorderWindow This page Puts a component in each of the five possible locations.
TabbedPaneDemo How to Use Tabbed Panes (in the Creating a GUI with JFC/Swing trail) One of many examples that puts a single component in the center of a content pane, so that the component is as large as possible.
CheckBoxDemo How to Use Check Boxes (in the Creating a GUI with JFC/Swing trail) Creates a JPanel that uses a BorderLayout. Puts components into the west and center locations.

Previous Page Lesson Contents Next Page Start of Tutorial > Start of Trail > Start of Lesson Search
Feedback Form

Copyright 1995-2003 Sun Microsystems, Inc. All rights reserved.