Handling MouseEvent with MouseListener Example


MouseEvent is handled by MouseListener overriding 5 abstract methods. Example given with Screenshot in Simple terms.

Java events are of two types – a) generated by frame, button etc. (AWT components) known as semantic events and b) generated by mouse, keyboard (Hardware components) known as low-level events. When you click a mouse, the mouse generates mouse event represented by class MouseEvent. Similarly, when you type a key on Keyboard, the key generates a key event represented by class KeyEvent and handled by KeyListener.

Generation of mouse events, again is of two types. If the mouse is stable (not moving) it generates MouseEvent and is handled by MouseListener and if the mouse is in motion, the mouse generates the same MouseEvent but handled by MouseMotionListener.

The mouse can generates five types of events (of course, when mouse stable only) and each type is represented by one abstract method of MouseListener. Following table illustrates.

Action Name Represented by abstract method of MouseListener
Mouse key is just pressed void mousePressed(MouseEvent)
Mouse key is released (after pressed) void mouseReleased(MouseEvent)
Mouse key is clicked void mouseClicked(MouseEvent)
When mouse enters a frame or MS Word window etc. void mouseEntered(MouseEvent)
When mouse exists a frame or MS Word window etc. (after entering) void mouseExited(MouseEvent)

This example illustrates how to handle the events when the mouse is stable using MouseListener by overriding the above 5 abstract methods. In this code, the mouse action is displayed as a label.

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

public class FrameMLDemo extends Frame implements MouseListener  
{                     
  Label lab;
  public FrameMLDemo()
  {
    add(lab = new Label(), "South");
    lab.setFont(new Font("Monospaced", Font.BOLD, 18));

    addMouseListener(this);
    
    setSize(300,300);
    setVisible(true);
  }                    
                         // override all the 5 abstract methods of MouseListener
  public void mousePressed(MouseEvent e)
  {  
    lab.setText("Mouse is pressed");
  }
  public void mouseReleased(MouseEvent e)
  {  
    lab.setText("Mouse is released");
  }
  public void mouseEntered(MouseEvent e)
  {  
    lab.setText("Mouse is entered");
  }
  public void mouseExited(MouseEvent e)
  {  
    lab.setText("Mouse is exited");
  }
  public void mouseClicked(MouseEvent e)
  {  
    lab.setText("Mouse is clicked");
  }
  
  public static void main(String snrao[])
  {
    new FrameMLDemo();
  }                                    
}     

addMouseListener(this);

This method defined in class Frame links or registers the frame with the MouseListener. If this statement is omitted, MouseListener does not handle the events of mouse and thereby no actions.

Label component is explained in AWT Label – Alignment.

All the five abstract methods of MouseListener are overridden.

Output screen when mouse exited the frame.
MouseEvent

Output screen shown with one of the actions when mouse clicked.

ima

Leave a Comment

Your email address will not be published.