Back to: Java Tutorials For Beginners and Professionals
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.
ActionListener Interface in Java
This interface defines the actionPerformed( ) method that is invoked when an action event occurs. Its general form is shown here:
void actionPerformed(ActionEvent ae)
AdjustmentListener Interface in Java
This interface defines the adjustmentValueChanged( ) method that is invoked when an adjustment event occurs. Its general form is shown here:
void adjustmentValueChanged(AdjustmentEvent ae)
ComponentListener Interface in Java
This interface defines four methods that are invoked when a component is resized, moved, shown, or hidden. Their general forms are shown here:
- void componentResized(ComponentEvent ce)
- void componentMoved(ComponentEvent ce)
- void componentShown(ComponentEvent ce)
- void componentHidden(ComponentEvent ce)
ContainerListener Interface in Java
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:
- void componentAdded(ContainerEvent ce)
- void componentRemoved(ContainerEvent ce)
FocusListener Interface in Java
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:
- void focusGained(FocusEvent fe)
- void focusLost(FocusEvent fe)
ItemListener Interface in Java
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)
KeyListener Interface in Java
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:
- void keyPressed(KeyEvent ke)
- void keyReleased(KeyEvent ke)
- void keyTyped(KeyEvent ke)
MouseListener Interface in Java
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:
- void mouseClicked(MouseEvent me)
- void mouseEntered(MouseEvent me)
- void mouseExited(MouseEvent me)
- void mousePressed(MouseEvent me)
- void mouseReleased(MouseEvent me)
MouseMotionListener Interface in Java
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:
- void mouseDragged(MouseEvent me)
- void mouseMoved(MouseEvent me)
MouseWheelListener Interface in Java
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)
TextListener Interface in Java
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)
WindowFocusListener Interface in Java
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:
- void windowGainedFocus(WindowEvent we)
- void windowLostFocus(WindowEvent we)
WindowListener Interface in Java
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:
- void windowActivated(WindowEvent we)
- void windowClosed(WindowEvent we)
- void windowClosing(WindowEvent we)
- void windowDeactivated(WindowEvent we)
- void windowDeiconified(WindowEvent we)
- void windowIconified(WindowEvent we)
- void windowOpened(WindowEvent we)
Adapter Classes in Java
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.
Example to Demonstrate Adapter Class in Java
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 in Java
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( ) with regard to the invoking object.
Example to demonstrate Inner Classes in java
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 in Java
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.
Example to demonstrate Anonymous Inner Class in Java
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.