awt

Determine when a component is added or removed from a container

In this example we shall show you how to use a ContainerListener in Java. When you develop an Application with dynamic GUI features, it’s very important to monitor the activities of the components that are added or removed from a component container, and that is the job of the ContainerListener.

In short to work with a ContainerListener you have to:

  • Create a new ContainerListener
  • Override the methods that correspond to the events that you want to monitor about the container e.g componentAddedcomponentRemoved and customize as you wish the handling of the respective events. Now every time a component is added or removed from the container, the corresponding method will be executed.
  • Use addContainerListener method to add the ContainerListener to the component you want to monitor.

Let’s take a look at the code snippet that follows:

package com.javacodegeeks.snippets.desktop;

import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Component;
import java.awt.Frame;
import java.awt.Panel;
import java.awt.TextArea;
import java.awt.event.ContainerAdapter;
import java.awt.event.ContainerEvent;
import java.awt.event.ContainerListener;

public class ContainerListenerExample {

  public static void main(String[] args) {

    // Create a frame
    Frame frame = new Frame("Example Frame");

    /**
     * Create a container with a flow layout, which arranges its children 
     * horizontally and center aligned.
     * A container can also be created with a specific layout using
     * Panel(LayoutManager) constructor, e.g.
     * Panel(new FlowLayout(FlowLayout.RIGHT)) for right alignment
     */
    Panel panel = new Panel();

    // Create a listen for container events
    ContainerListener listener = new ContainerAdapter() {

  public void componentAdded(ContainerEvent evt) {

    // Get component that was added

    Component c = evt.getChild();

    System.out.println("Component " + c + " added to panel");

  }

  public void componentRemoved(ContainerEvent evt) {

    // Get component that was removed

    Component c = evt.getChild();

    System.out.println("Component " + c + " removed from panel");

  }
    };

    // Register the listener with the container
    panel.addContainerListener(listener);

    // Add several buttons to the container
    panel.add(new Button("Button_A"));
    panel.add(new Button("Button_B"));
    panel.add(new Button("Button_C"));

    // Add a text area in the center of the frame
    Component textArea = new TextArea("This is a sample text...");
    frame.add(textArea, BorderLayout.CENTER);

    // Add the container to the bottom of the frame
    frame.add(panel, BorderLayout.SOUTH);

    // Display the frame
    int frameWidth = 300;
    int frameHeight = 300;
    frame.setSize(frameWidth, frameHeight);

    frame.setVisible(true);

  }

}

Example Output:

Component java.awt.Button[button0,0,0,0x0,invalid,label=Button_A] added to pannel
Component java.awt.Button[button1,0,0,0x0,invalid,label=Button_B] added to pannel
Component java.awt.Button[button2,0,0,0x0,invalid,label=Button_C] added to pannel

 
This was an example on how to determine when a component is added or removed from a container.

Ilias Tsagklis

Ilias is a software developer turned online entrepreneur. He is co-founder and Executive Editor at Java Code Geeks.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button