Download the JDK
Search the Tutorials
Hide the TOC
Trail: Creating a GUI with JFC/Swing
Lesson: Writing Event Listeners
Section: Implementing Listeners for Commonly Handled Events
Writing Event Listeners
Introduction to Event Listeners
General Information about Writing Event Listeners
Listeners Supported by Swing Components
Implementing Listeners for Commonly Handled Events
How to Write an Action Listener
How to Write a Caret Listener
How to Write a Change Listener
How to Write a Component Listener
How to Write a Container Listener
How to Write a Document Listener
How to Write a Focus Listener
How to Write an Internal Frame Listener
How to Write an Item Listener
How to Write a Key Listener
How to Write a List Data Listener
How to Write a List Selection Listener
How to Write a Mouse Listener
How to Write a Mouse-Motion Listener
How to Write a Mouse-Wheel Listener
How to Write a Property Change Listener
How to Write a Table Model Listener
How to Write a Tree Expansion Listener
How to Write a Tree Model Listener
How to Write a Tree Selection Listener
How to Write a Tree-Will-Expand Listener
How to Write an Undoable Edit Listener
How to Write Window Listeners
Listener API Table
Solving Common Event-Handling Problems
Home Page > Creating a GUI with JFC/Swing > Writing Event Listeners
How to Write a Component Listener

> The Component listener is a listener interface for receiving component events. A component is an object having a graphical representation that can be displayed on the screen and that can interact with the user. Some of the examples of components are the buttons, checkboxes, and scrollbars of a typical graphical user interface.

> The class that is interested in processing a component event either implements this interface and all the methods it contains, or extends the abstract ComponentAdapter class overriding only the methods of interest. The listener object created from that class is then registered with a component using the component's addComponentListener method. When the component's size, location, or visibility changes, the relevant method in the listener object is invoked, and the ComponentEvent is passed to it.

> One or more component events are fired by a `Component` object just after the component is hidden, made visible, moved, or resized.

> The component-hidden and component-shown events occur only as the result of calls to a `Component` 's `setVisible` method. For example, a window might be miniaturized into an icon (iconified) without a component-hidden event being fired.

> To write a simple Component listener program, follow the steps mentioned below:

> - Declare a class which implements Component listener. For example:

>   >     public class ComponentEventDemo ... implements ComponentListener

> - Identify the components that you would like to catch the events for. For example: pane, label, checkbox, etc.
> - Add the Component Listener to the identified components. For example:

>   >     ....
>   >     label.addComponentListener(this);
>   >     .....
>   >     checkbox.addComponentListener(this);
>   >     ....
>   >     panel.addComponentListener(this);
>   >     ...
>   >     frame.addComponentListener(this);

> - Finally, catch the different events of these components by using four methods of Component Listener as shown below:

>   >     public void componentHidden(ComponentEvent e) {
>   >             displayMessage(e.getComponent().getClass().getName() + " --- Hidden");
>   >         }

>   >         public void componentMoved(ComponentEvent e) {
>   >             displayMessage(e.getComponent().getClass().getName() + " --- Moved");
>   >         }

>   >         public void componentResized(ComponentEvent e) {
>   >             displayMessage(e.getComponent().getClass().getName() + " --- Resized ");
>   >         }

>   >         public void componentShown(ComponentEvent e) {
>   >             displayMessage(e.getComponent().getClass().getName() + " --- Shown");

>   >         }

> The following example demonstrates component events. The window contains a panel that has a label and a check box. The check box controls whether the label is visible. A text area displays a message every time the window, panel, label, or check box fires a component event.

> > Try this:
> > 1. Click the Launch button to run ComponentEventDemo using Java™ Web Start (download JDK 6). Alternatively, to compile and run the example yourself, consult the example index.

> > 2. When the window appears, one or more component-shown events have been fired.
> > 3. Click the check box to hide the label.
> >    The label fires a component-hidden event. The panel fires component-moved and component-resized events. The check box fires a component-moved event.
> > 4. Click the check box again to show the label.
> >    The label fires a component-shown event. The panel fires component-moved and component-resized events. The check box fires a component-moved event.
> > 5. Iconify and then deiconify the window.
> >    You do not get component-hidden or -shown events. If you want to be notified of iconification events, you should use a window listener or a window state listener.
> > 6. Resize the window.
> >    You will see component-resized (and possibly component-moved) events from all four components — label, check box, panel, and frame. If the frame and panel's layout manager did not make every component as wide as possible, the panel, label, and check box would not have been resized.

> You can find the demo's code in `ComponentEventDemo.java`. Here is just the code related to handling component events:

> >     public class ComponentEventDemo ... implements ComponentListener {
> >         static JFrame frame;
> >         JLabel label;
> >         ...
> >         public ComponentEventDemo() {
> >             ...
> >             JPanel panel = new JPanel(new BorderLayout());
> >             label = new JLabel("This is a label", JLabel.CENTER);
> >             label.addComponentListener(this);
> >             panel.add(label, BorderLayout.CENTER);

> >             JCheckBox checkbox = new JCheckBox("Label visible", true);
> >             checkbox.addComponentListener(this);
> >             panel.add(checkbox, BorderLayout.PAGE_END);
> >             panel.addComponentListener(this);
> >             ...
> >             frame.addComponentListener(this);
> >         }
> >         ...
> >          public void componentHidden(ComponentEvent e) {
> >             displayMessage(e.getComponent().getClass().getName() + " --- Hidden");
> >         }

> >         public void componentMoved(ComponentEvent e) {
> >             displayMessage(e.getComponent().getClass().getName() + " --- Moved");
> >         }

> >         public void componentResized(ComponentEvent e) {
> >             displayMessage(e.getComponent().getClass().getName() + " --- Resized ");
> >         }

> >         public void componentShown(ComponentEvent e) {
> >             displayMessage(e.getComponent().getClass().getName() + " --- Shown");

> >         }

> >         public static void main(String[] args) {
> >             ...
> >             //Create and set up the window.
> >             frame = new JFrame("ComponentEventDemo");
> >             ...
> >             JComponent newContentPane = new ComponentEventDemo();
> >             frame.setContentPane(newContentPane);
> >             ...
> >         }
> >     }

### The Component Listener API

> The ComponentListener Interface

> All of these methods are also in the adapter class, `ComponentAdapter`.

> Method | Purpose
> --- | ---
> componentHidden(ComponentEvent) | Called after the listened-to component is hidden as the result of the `setVisible` method being called.
> componentMoved(ComponentEvent) | Called after the listened-to component moves, relative to its container. For example, if a window is moved, the window fires a component-moved event, but the components it contains do not.
> componentResized(ComponentEvent) | Called after the listened-to component's size (rectangular bounds) changes.
> componentShown(ComponentEvent) | Called after the listened-to component becomes visible as the result of the `setVisible` method being called.

> The ComponentEvent Class

> Method | Purpose
> --- | ---
> Component getComponent() | Returns the component that fired the event. You can use this instead of the `getSource` method.

### Examples that Use Component Listeners

> The following table lists the examples that use component listeners.

> Example | Where Described | Notes
> --- | --- | ---
> `ComponentEventDemo` | This section | Reports all component events that occur on several components to demonstrate the circumstances under which component events are fired.

Previous page: How to Write a Change Listener
Next page: How to Write a Container Listener