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: Using Swing Components

How to Use Panels

The JPanel(in the API reference documentation) class provides general-purpose containers for lightweight components. By default, panels don't paint anything except for their background; however, you can easily add borders to them and otherwise customize their painting. For information about using panels to perform custom painting, see Overview of Custom Painting(in the Creating a GUI with JFC/Swing trail).

By default, panels are opaque. This makes them work well as content panes, and can help painting efficiency, as described in Painting(in the Creating a GUI with JFC/Swing trail). You can make a panel transparent by invoking setOpaque(false). A transparent panel draws no background, so that any components underneath show through.

An Example

The Converter application described in The Anatomy of a Swing-Based Program(in the Creating a GUI with JFC/Swing trail) uses panels in several ways: The following picture is the same as a picture in The Anatomy of a Swing-Based Program(in the Creating a GUI with JFC/Swing trail). It shows a colorized version of Converter, in which each panel has a background color.

Colorful Converter

As the figure shows, panels are useful for grouping components, simplifying component layout, and putting borders around groups of components. The rest of this section gives hints on grouping and laying out components. For information about using borders, see How to Use Borders(in the Creating a GUI with JFC/Swing trail).

Setting the Layout Manager

Like other containers, a panel uses a layout manager to position and size its components. By default, a panel's layout manager is an instance of FlowLayout(in the Creating a GUI with JFC/Swing trail), which places the panel's contents in a row. You can easily make a panel use any other layout manager by invoking the setLayout method or specifying a layout manager when creating the panel. Here is an example of the first approach:
JPanel aPanel = new JPanel();
aPanel.setLayout(new BorderLayout());
Here is an example of setting the layout manager at instantiation:
JPanel aPanel = new JPanel(new BorderLayout());

For more information about choosing and using layout managers, see Laying Out Components Within a Container(in the Creating a GUI with JFC/Swing trail).

Adding Components

When you add components to a panel, you use the add method. Exactly which arguments you specify to the add method depend on which layout manager the panel uses. When the layout manager is FlowLayout, BoxLayout, GridLayout, or GridBagLayout, you'll typically use the one-argument add method, like this:
aFlowPanel.add(aComponent);
aFlowPanel.add(anotherComponent);
When the layout manager is BorderLayout, you need to provide a second argument specifying the added component's position within the panel. For example:
aBorderPanel.add(aComponent, BorderLayout.CENTER);
aBorderPanel.add(anotherComponent, BorderLayout.SOUTH);
For information on the arguments to use with BorderLayout, see How to Use Borders(in the Creating a GUI with JFC/Swing trail).

The Panel API

The API in the JPanel class itself is minimal. The methods you are most likely to invoke on a JPanel object are those it inherits from its superclasses -- JComponent(in the API reference documentation), Container(in the API reference documentation), and Component(in the API reference documentation). The following tables list the API you're most likely to use, with the exception of methods related to borders and layout hints. For more information about the API all JComponents can use, see The JComponent Class.

Creating a JPanel
Constructor Purpose
JPanel()
JPanel(LayoutManager)
Create a panel. The LayoutManager parameter provides a layout manager for the new panel. By default, a panel uses a FlowLayout to lay out its components.

Managing a Container's Components
Method Purpose
void add(Component)
void add(Component, int)
void add(Component, Object)
void add(Component, Object, int)
void add(String, Component)
Add the specified component to the panel. When present, the int parameter is the index of the component within the container. By default, the first component added is at index 0, the second is at index 1, and so on. The Object parameter is layout manager dependent and typically provides information to the layout manager regarding positioning and other layout constraints for the added component. The String parameter is similar to the Object parameter.
int getComponentCount() Get the number of components in this panel.
Component getComponent(int)
Component getComponentAt(int, int)
Component getComponentAt(Point)
Component[] getComponents()
Get the specified component or components. You can get a component based on its index or x, y position.
void remove(Component)
void remove(int)
void removeAll()
Remove the specified component(s).

Setting/Getting the Layout Manager
Method Purpose
void setLayout(LayoutManager)
LayoutManager getLayout()
Set or get the layout manager for this panel. The layout manager is responsible for positioning the panel's components within the panel's bounds according to some philosophy.

Examples that Use Panels

Many examples contained in this lesson use JPanel objects. The following table lists a few.

Example Where Described Notes
Converter
and other files
This section and The Anatomy of a Swing-Based Program(in the Creating a GUI with JFC/Swing trail) Uses five panels, four of which use BoxLayout and one of which uses GridLayout. The panels use borders and, as necessary, size and alignment hints to affect layout.
ListDemo How to Use Lists Uses a panel, with its default FlowLayout manager, to center three components in a row.
ToolBarDemo How to Use Toolbar Uses a panel as a content pane. The panel contains three components, laid out by BorderLayout.
BorderDemo How to Use Borders Contains many panels that have various kinds of borders. Several panels use BoxLayout.
BoxLayoutDemo How to Use BoxLayout Illustrates the use of a panel with Swing's BoxLayout manager.


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

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