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: Writing Event Listeners

How to Write a Container Listener

Container events are fired by a Container just after a component is added to or removed from the container. These events are for notification only -- no container listener need be present for components to be successfully added or removed.

The following applet demonstrates container events. By clicking Add a button or Remove a button, you can add components to or remove them from a panel at the bottom of the applet. Each time a component is added to or removed from the panel, the panel fires a container event, and the panel's container listener is notified. The listener displays descriptive messages in the text area at the top of the applet.

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.


Try this: 
  1. Click the button labeled Add a button.
    You'll see a button appear near the bottom of the applet. The container listener (in this example, an instance of ContainerEventDemo) reacts to the resulting component-added event by displaying "JButton #1 was added to javax.swing.JPanel" at the top of the applet.
  2. Click the button labeled Remove a button.
    This removes the most recently added button from the panel, causing the container listener to receive a component-removed event.

You can find the applet's code in ContainerEventDemo.java(in a .java source file). Here is the applet's container event handling code:

public class ContainerEventDemo ... implements ContainerListener ... {
    ...//where initialization occurs:
	buttonPanel = new JPanel();
	buttonPanel.addContainerListener(this);
    ...
    public void componentAdded(ContainerEvent e) {
	displayMessage(" added to ", e);
    }

    public void componentRemoved(ContainerEvent e) {
	displayMessage(" removed from ", e);
    }

    void displayMessage(String action, ContainerEvent e) {
	display.append(((JButton)e.getChild()).getText()
		       + " was"
		       + action
		       + e.getContainer().getClass().getName()
		       + newline);
    }
    ...
}

The Container Event API

The ContainerListener(in the API reference documentation) interface and its corresponding adapter class, ContainerAdapter(in the API reference documentation), contain two methods:
void componentAdded(ContainerEvent)
Called just after a component is added to the listened-to container.
void componentRemoved(ContainerEvent)
Called just after a component is removed from the listened-to container.
Each container event method has a single parameter: a ContainerEvent(in the API reference documentation) object. The ContainerEvent class defines two useful methods:
Component getChild()
Returns the component whose addition or removal triggered this event.
Container getContainer()
Returns the container that fired this event. You can use this instead of the getSource method.

Examples that Use Container Listeners

The following table lists the examples that use container listeners.

Example Where Described Notes
ContainerEventDemo This section Reports all container events that occur on a single panel to demonstrate the circumstances under which container events are fired.


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

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