How to Write a Container Listener

 < Day Day Up > 

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

Figure 3 demonstrates container events. By clicking Add a button or Remove a button , you can add buttons to or remove them from a panel at the bottom of the window. Each time a button 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 window.

Figure 3. The ContainerEventDemo application.

graphics/10fig03.gif

Try This:

  1. graphics/cd_icon.gif

    Run ContainerEventDemo using Java Web Start or compile and run the example yourself. [6]

    [6] To run ContainerEventDemo using Java Web Start, click the ContainerEventDemo link on the RunExamples/events.html page on the CD. You can find the source files here: JavaTutorial/uiswing/events/example-1dot4/index.html#ContainerEventDemo .

  2. Click the button labeled Add a button . You'll see a button appear near the bottom of the window. The container listener reacts to the resulting component-added event by displaying " JButton #1 was added to javax.swing.JPanel " at the top of the window.

  3. 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 demo's code in ContainerEventDemo.java . Here's the demo's container event-handling code:

 public class ContainerEventDemo ... implements ContainerListener ... {  ...//where initialization occurs:  buttonPanel = new JPanel(new GridLayout(1,1));         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 Listener API

Table 9 lists the methods in the ContainerListener interface and Table 10 describes the methods in the ContainerEvent class. Also refer to the API documentation for ContainerListener at: http://java.sun.com/j2se/1.4.2/docs/api/java/awt/event/ContainerListener.html. The API documentation for ContainerEvent is online at: http://java.sun.com/j2se/1.4.2/docs/api/java/awt/event/ContainerEvent.html.

Table 9. The ContainerListener Interface (The corresponding adapter class is ContainerAdapter . [a] )

Method

Purpose

componentAdded(ContainerEvent)

Called just after a component is added to the listened-to container.

componentRemoved(ContainerEvent)

Called just after a component is removed from the listened-to container.

[a] ContainerAdapter API documentation: http://java.sun.com/j2se/1.4.2/docs/api/java/awt/event/ContainerAdapter.html.

Table 10. The ContainerEvent Class

Method

Purpose

Component getChild()

Return the component whose addition or removal triggered this event.

Container getContainer()

Return the container that fired this event. You can use this instead of the getSource method.

Examples That Use Container Listeners

The following examples 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.

 < Day Day Up > 


JFC Swing Tutorial, The. A Guide to Constructing GUIs
The JFC Swing Tutorial: A Guide to Constructing GUIs (2nd Edition)
ISBN: 0201914670
EAN: 2147483647
Year: 2004
Pages: 171

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net