KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > java > awt > event > ContainerEvent


1 /*
2  * @(#)ContainerEvent.java 1.18 03/12/19
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package java.awt.event;
9
10 import java.awt.AWTEvent JavaDoc;
11 import java.awt.Container JavaDoc;
12 import java.awt.Component JavaDoc;
13
14 /**
15  * A low-level event which indicates that a container's contents
16  * changed because a component was added or removed.
17  * <P>
18  * Container events are provided for notification purposes ONLY;
19  * The AWT will automatically handle changes to the containers
20  * contents internally so that the program works properly regardless of
21  * whether the program is receiving these events or not.
22  * <P>
23  * This low-level event is generated by a container object (such as a
24  * Panel) when a component is added to it or removed from it.
25  * The event is passed to every <code>ContainerListener</code>
26  * or <code>ContainerAdapter</code> object which registered to receive such
27  * events using the component's <code>addContainerListener</code> method.
28  * (<code>ContainerAdapter</code> objects implement the
29  * <code>ContainerListener</code> interface.) Each such listener object
30  * gets this <code>ContainerEvent</code> when the event occurs.
31  *
32  * @see ContainerAdapter
33  * @see ContainerListener
34  * @see <a HREF="http://java.sun.com/docs/books/tutorial/post1.0/ui/containerlistener.html">Tutorial: Writing a Container Listener</a>
35  * @see <a HREF="http://www.awl.com/cp/javaseries/jcl1_2.html">Reference: The Java Class Libraries (update file)</a>
36  *
37  * @author Tim Prinzing
38  * @author Amy Fowler
39  * @version 1.18 12/19/03
40  * @since 1.1
41  */

42 public class ContainerEvent extends ComponentEvent JavaDoc {
43
44     /**
45      * The first number in the range of ids used for container events.
46      */

47     public static final int CONTAINER_FIRST = 300;
48
49     /**
50      * The last number in the range of ids used for container events.
51      */

52     public static final int CONTAINER_LAST = 301;
53
54    /**
55      * This event indicates that a component was added to the container.
56      */

57     public static final int COMPONENT_ADDED = CONTAINER_FIRST;
58
59     /**
60      * This event indicates that a component was removed from the container.
61      */

62     public static final int COMPONENT_REMOVED = 1 + CONTAINER_FIRST;
63
64     /**
65      * The non-null component that is being added or
66      * removed from the Container.
67      *
68      * @serial
69      * @see #getChild()
70      */

71     Component JavaDoc child;
72
73     /*
74      * JDK 1.1 serialVersionUID
75      */

76     private static final long serialVersionUID = -4114942250539772041L;
77
78     /**
79      * Constructs a <code>ContainerEvent</code> object.
80      * <p>Note that passing in an invalid <code>id</code> results in
81      * unspecified behavior. This method throws an
82      * <code>IllegalArgumentException</code> if <code>source</code>
83      * is <code>null</code>.
84      *
85      * @param source the <code>Component</code> object (container)
86      * that originated the event
87      * @param id an integer indicating the type of event
88      * @param child the component that was added or removed
89      * @throws IllegalArgumentException if <code>source</code> is null
90      */

91     public ContainerEvent(Component JavaDoc source, int id, Component JavaDoc child) {
92         super(source, id);
93         this.child = child;
94     }
95
96     /**
97      * Returns the originator of the event.
98      *
99      * @return the <code>Container</code> object that originated
100      * the event, or <code>null</code> if the object is not a
101      * <code>Container</code>.
102      */

103     public Container JavaDoc getContainer() {
104         return (source instanceof Container JavaDoc) ? (Container JavaDoc)source : null;
105     }
106
107     /**
108      * Returns the component that was affected by the event.
109      *
110      * @return the Component object that was added or removed
111      */

112     public Component JavaDoc getChild() {
113         return child;
114     }
115
116     /**
117      * Returns a parameter string identifying this event.
118      * This method is useful for event-logging and for debugging.
119      *
120      * @return a string identifying the event and its attributes
121      */

122     public String JavaDoc paramString() {
123         String JavaDoc typeStr;
124         switch(id) {
125           case COMPONENT_ADDED:
126               typeStr = "COMPONENT_ADDED";
127               break;
128           case COMPONENT_REMOVED:
129               typeStr = "COMPONENT_REMOVED";
130               break;
131           default:
132               typeStr = "unknown type";
133         }
134         return typeStr + ",child="+child.getName();
135     }
136 }
137
Popular Tags