The Java Developers Almanac 1.4


Order this book from Amazon.

   
Home > List of Packages > java.awt  [78 examples] > Containers  [3 examples]

e573. Determining When a Component Is Added or Removed from a Container

A container fires a container event whenever a component is added or removed.
    // Create a listen for container events
    ContainerListener listener = new ContainerAdapter() {
        public void componentAdded(ContainerEvent evt) {
            // Get component that was added
            Component c = evt.getChild();
        }
        public void componentRemoved(ContainerEvent evt) {
            // Get component that was removed
            Component c = evt.getChild();
        }
    };
    
    // Register the listener with the container
    container.addContainerListener(listener);

 Related Examples
e571. Creating a Container
e572. Getting the Child Components of a Container

See also: Colors    Components    Cursors    Drawing    Events    Focus    Frames    GridBagLayout    Images    Shapes    Simulating Events    Text    The Screen   


© 2002 Addison-Wesley.