Event Listener Interfaces in Java

Event Listener Interfaces in Java

In this article, I am going to discuss Event Listener Interfaces in Java with Examples. Please read our previous article, where we discussed Event Handling in Java with Examples.  At the end of this article, you will understand all about Java Event Listener Interfaces with Examples.

Event Listener Interfaces in Java:

Listeners are created by implementing one or more of the interfaces defined by the java.awt.event package. When an event occurs, the event source invokes the appropriate method defined by the listener and provides an event object as its argument.

The ActionListener Interface

This interface defines the actionPerformed( ) method that is invoked when an action event occurs. Its general form is shown here:

void actionPerformed(ActionEvent ae)

The AdjustmentListener Interface

This interface defines the adjustmentValueChanged( ) method that is invoked when an adjustment event occurs. Its general form is shown here:

void adjustmentValueChanged(AdjustmentEvent ae)

The ComponentListener Interface

This interface defines four methods that are invoked when a component is resized, moved, shown, or hidden. Their general forms are shown here:

  1. void componentResized(ComponentEvent ce)
  2. void componentMoved(ComponentEvent ce)
  3. void componentShown(ComponentEvent ce)
  4. void componentHidden(ComponentEvent ce)
The ContainerListener Interface

This interface contains two methods. When a component is added to a container, componentAdded( ) is invoked. When a component is removed from a container, componentRemoved( ) is invoked. Their general forms are shown here:

  1. void componentAdded(ContainerEvent ce)
  2. void componentRemoved(ContainerEvent ce)
The FocusListener Interface

This interface defines two methods. When a component obtains keyboard focus, focusGained() is invoked. When a component loses keyboard focus, focusLost( ) is called. Their general forms are shown here:

  1. void focusGained(FocusEvent fe)
  2. void focusLost(FocusEvent fe)
The ItemListener Interface

This interface defines the itemStateChanged( ) method that is invoked when the state of an item changes. Its general form is shown here:

void itemStateChanged(ItemEvent ie)

The KeyListener Interface

This interface defines three methods. The keyPressed( ) and keyReleased( ) methods are invoked when a key is pressed and released, respectively. The keyTyped() method is invoked when a character has been entered. The general forms of these methods are shown here:

  1. void keyPressed(KeyEvent ke)
  2. void keyReleased(KeyEvent ke)
  3. void keyTyped(KeyEvent ke)
The MouseListener Interface

This interface defines five methods. If the mouse is pressed and released at the same point, mouseClicked( ) is invoked. When the mouse enters a component, the mouseEntered( ) method is called. When it leaves, mouseExited( ) is called. The mousePressed( ) and mouseReleased() methods are invoked when the mouse is pressed and released, respectively. The general forms of these methods are shown here:

  1. void mouseClicked(MouseEvent me)
  2. void mouseEntered(MouseEvent me)
  3. void mouseExited(MouseEvent me)
  4. void mousePressed(MouseEvent me)
  5. void mouseReleased(MouseEvent me)
The MouseMotionListener Interface

This interface defines two methods. The mouseDragged( ) method is called multiple times as the mouse is dragged. The mouseMoved( ) method is called multiple times as the mouse is moved. Their general forms are shown here:

  1. void mouseDragged(MouseEvent me)
  2. void mouseMoved(MouseEvent me)
The MouseWheelListener Interface

This interface defines the mouseWheelMoved( ) method that is invoked when the mouse wheel is moved. Its general form is shown here:

void mouseWheelMoved(MouseWheelEvent mwe)

The TextListener Interface

This interface defines the textChanged( ) method that is invoked when a change occurs in a text area or text field. Its general form is shown here:

void textChanged(TextEvent te)

The WindowFocusListener Interface

This interface defines two methods: windowGainedFocus() and windowLostFocus().These are called when a window gains or loses input focus.

Their general forms are shown here:

  1. void windowGainedFocus(WindowEvent we)
  2. void windowLostFocus(WindowEvent we)
The WindowListener Interface

This interface defines seven methods. The windowActivated( ) and windowDeactivated( ) methods are invoked when a window is activated or deactivated, respectively. If a window is iconified, the windowIconified( ) method is called. When a window is deiconified, the windowDeiconified( ) method is called. When a window is opened or closed, the windowOpened() or windowClosed() methods are called, respectively. The windowClosing() method is called when a window is being closed. The general forms of these methods are:

  1. void windowActivated(WindowEvent we)
  2. void windowClosed(WindowEvent we)
  3. void windowClosing(WindowEvent we)
  4. void windowDeactivated(WindowEvent we)
  5. void windowDeiconified(WindowEvent we)
  6. void windowIconified(WindowEvent we)
  7. void windowOpened(WindowEvent we)
Adapter Classes

Java provides a special feature, called an adapter class, which will simplify the creation of event handlers in certain situations. An adapter class provides an empty implementation of all methods in an occasion listener interface. Adapter classes are useful once you want to receive and process just some of the events that are handled by a specific event listener interface. You can define a replacement class to act as an occasion listener by extending one among the adapter classes and implementing only those events during which you’re interested.

Sample Program to demonstrate Adapter Class
import java.awt.*;
import java.awt.event.*;
import java.applet.*;

/*
<applet code="AdapterClassDemo" width=300 height=100> </applet>
*/
public class AdapterClassDemo extends Applet
{
    public void init ()
    {
        addMouseListener (new MyMouseAdapter (this));
        addMouseMotionListener (new MyMouseMotionAdapter (this));
    }
}

class MyMouseAdapter extends MouseAdapter
{
    AdapterClassDemo adapterDemo;

    public MyMouseAdapter (AdapterClassDemo adapterDemo)
    {
        this.adapterDemo = adapterDemo;
    }
    
    // Handle mouse clicked. 
    public void mouseClicked (MouseEvent me)
    {
        adapterDemo.showStatus ("Mouse clicked");
    }
}

class MyMouseMotionAdapter extends MouseMotionAdapter
{
    AdapterClassDemo adapterDemo;

    public MyMouseMotionAdapter (AdapterClassDemo adapterDemo)
    {
        this.adapterDemo = adapterDemo;
    }

    // Handle mouse dragged. 
    public void mouseDragged (MouseEvent me)
    {
        adapterDemo.showStatus ("Mouse dragged");
    }
}

As you can see by looking at the program, not having to implement all of the methods defined by the MouseMotionListener and MouseListener interfaces saves you a considerable amount of effort and prevents your code from becoming cluttered with empty methods.

Inner Classes

An inner class may be a class defined within another class, or maybe within an expression.

Consider the applet shown within the following listing. It doesn’t use an inner class. Its goal is to display the string “MousePressed” within the status bar of the applet viewer or browser when the mouse is pressed. There are two top-level classes during this program. InnerClassDemo extends Applet, and MyMouseAdapter extends MouseAdapter. Because MyMouseAdapter is defined within the scope of InnerClassDemo, it’s access to all or any of the variables and methods within the scope of that class. Therefore, the mousePressed() method can call the showStatus() method directly. It not must do that via stored regard to the applet. Thus, it’s not necessary to pass MyMouseAdapter( ) regard to the invoking object.

Sample Program to demonstrate Inner Class
import java.applet.*;
import java.awt.event.*;
/* <applet code="InnerClassDemo" width=200 height=100> </applet> */

public class InnerClassDemo extends Applet
{
    public void init ()
    {
        addMouseListener (new MyMouseAdapter ());
    }

    class MyMouseAdapter extends MouseAdapter
    {
        public void mousePressed (MouseEvent me)
        {
         	showStatus ("Mouse Pressed");
        }
    }
}
Anonymous Inner Class

An anonymous inner class is not assigned a name. This section illustrates how an anonymous inner class can facilitate the writing of event handlers. Consider the applet shown in the following listing. As before, its goal is to display the string “Mouse Pressed” in the status bar of the applet viewer or browser when the mouse is pressed.

Sample Program to demonstrate Anonymous Inner Class
import java.applet.*;
import java.awt.event.*;
/* <applet code="AnonymousInnerClassDemo" width=200 height=100> </applet> */
public class AnonymousInnerClassDemo extends Applet
{
    public void init ()
    {
        addMouseListener (new MouseAdapter ()
  {
        public void mousePressed (MouseEvent me)
        {
        showStatus ("Mouse Pressed");}
   });
    }
}

There is one top-level class during this program: AnonymousInnerClassDemo. The init( ) method is used to call the addMouseListener( ) method. Its argument defines and instantiates an anonymous inner class. Let’s analyze this expression carefully.

The syntax new MouseAdapter(){…} indicates the compiler that the code between the braces defines an anonymous inner class. Furthermore, that class extends MouseAdapter. This new class is not named, but it’s automatically instantiated when this expression is executed. Because this anonymous inner class is defined within the scope of AnonymousInnerClassDemo, it’s access to all or any of the variables and methods within the scope of that class. Therefore, it calls the showStatus( ) method directly.

In the next article, I am going to discuss the Layout Manager in Java with examples. Here, in this article, I try to explain Event Listener Interfaces in Java and I hope you enjoy this Event Listener Interfaces in Java article.

Leave a Reply

Your email address will not be published. Required fields are marked *