Previous | Next | Trail Map | Creating a GUI with JFC/Swing | 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.

The following code creates the BorderLayout and the components it manages. Here's the whole program. The program runs either within an applet, with the help of AppletButton, 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)"),
                BorderLayout.NORTH);
contentPane.add(new JButton("2 (CENTER)"),
                BorderLayout.CENTER);
contentPane.add(new JButton("Button 3 (WEST)"),
                BorderLayout.WEST);
contentPane.add(new JButton("Long-Named Button 4 (SOUTH)"),
                BorderLayout.SOUTH);
contentPane.add(new JButton("Button 5 (EAST)"),
                BorderLayout.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)  //we prefer this form
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
    or
add("Center", component)             //valid but error prone

By default, a BorderLayout puts no gap between the components it manages. In the preceding applet, any apparent gaps are the result of the JButtons reserving extra space around their apparent display area. You can specify gaps (in pixels) using the following constructor:

public BorderLayout(int horizontalGap, int verticalGap)

Examples that Use BorderLayout

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

Example Where Described Notes
BorderWindow.java This page Puts a component in each of the five possible locations.
TabbedPaneDemo.java How to Use Tabbed Panes(in the Creating a User Interface 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.java How to Use Check Boxes(in the Creating a User Interface trail) Creates a JPanel that uses a BorderLayout Puts components into the west and center locations.
ComboBoxDemo.java How to Use Combo Boxes(in the Creating a User Interface trail) Puts components into the north and south locations of a content pane, leaving the center empty.


Previous | Next | Trail Map | Creating a GUI with JFC/Swing | Laying Out Components Within a Container