This is same as what we did in previous tutorial on “ActionListener – Click event handling“.
Event Listener is implemented in the class where we have components like Button, Label, etc. In this implementation, we have to add all the methods of Listener in the same class.

Characteristics:

  1. Components/Event Sources can be easily accessed from Listener methods
  2. Event can be applied to any component by passing current object(this). No need of creating other object.
  3. View as well as Logic is combined in single class which is a bad practice.
  4. All the methods must be defined. Blank definition is to be given even if we don’t want to use it.

Code:

import java.awt.*;
import java.applet.*;
import java.awt.event.*;

public class EventDemo1 extends Applet implements MouseListener
{
	Button btn;
	Label lbl;
	public void init()
	{
		btn = new Button("Click Me!");
		lbl = new Label("          ");
		add(btn);
		add(lbl);
		btn.addMouseListener(this);
	}	
	public void mouseClicked(MouseEvent e)
	{
	 lbl.setText("Clicked!");
	} 
	public void mouseEntered(MouseEvent e){}
	public void mouseExited(MouseEvent e){}
	public void mousePressed(MouseEvent e){} 
	public void mouseReleased(MouseEvent e){} 
 }
/*
<applet code="EventDemo1" width="500" height="50"></applet>
*/