Home : Course Map : Chapter 7 : Java :
Mouse Listeners
JavaTech
Course Map
Chapter 7

Introduction
Event Overview
Event Processing
Button Events
  Demo 1
 Demo 2
Mouse Events
  Demo3

More Components
  Demo 4  Demo 5
  Demo 6  Demo 7

LayoutManagers-1
  Demo 8     Demo 9
  Demo 10  Demo 11
  Demo 12

LayoutManagers-2
  Demo 13  Demo 14
  Demo 15  Demo 16
  Demo 17

Inner Classes
Anonymous Class
Adapter Classes
  Demo 18  Demo 19
Frames & Menus
  Demo 20  Demo 21
Exercises

    Supplements
AWT Components
  Button
     Demo 1
  Canvas
     Demo 2
  AWT GUI Demo
     Demo 3
Swing Dialogs
JOptionPane Dialog
  Demo 1
JDialog
  Demo 2
UI Enhancement: P1
  Demo 1   Demo 2
  Demo 3

UI Enhancement: P2
  Demo 1
     About JavaTech
     Codes List
     Exercises
     Feedback
     References
     Resources
     Tips
     Topic Index
     Course Guide
     What's New

Responding to mouse events involves two kinds of listeners :

MouseMotionListener
Each move of the mouse generates a motion event. To listen for mouse motion events, a class needs to implement the MouseMotionListener interface. The class will need to override two methods:

  • MouseDragged(MouseEvent e)- mouse motion when button pressed
  • MouseMoved(MouseEvent e)

MouseListener
Send events whenever the mouse

  • button is pressed
  • button is released
  • clicked (press & release counted as one action)
  • enters the area of the component
  • exits the area of the component

The MouseListener interface thus provides a method that corresponds to each of these types of events:

  • mousePressed(MouseEvent e)
  • mouseReleased(MouseEvent e)
  • mouseClicked(MouseEvent e)
  • mouseEntered(MouseEvent e)
  • mouseExited(MouseEvent e)

The following example illustrates how to use the MouseListener interface to monitor mouse clicks over a panel and to indicate when the cursor enters or exits the area of the panel.

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

/**  Demonstrate a MouseListener component. **/
public class CaptureEvtApplet extends JApplet {

  /** Create the interface with CaptureEventPanel. **/
  public void init () {
    Container content_pane = getContentPane ();

    // Create an instance of the JPanel subclass
    CaptureEventPanel cap_evt_panel = new CaptureEventPanel ();

    // And the panel to the JApplet panel.
    content_pane.add (cap_evt_panel);

  } // init

} // class CaptureEvtApplet

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

/** This JPanel subclass uses MouseListener to capture mouse events **/
public class CaptureEventPanel extends JPanel
                   implements MouseListener{

  JTextArea fTextOutput;
  String newline;

  /**
    * Constructor adds this class to the MouseListener list
    * for a panel and sends messages to a text area whenever
    * an event occurs over the panel.
   **/
  CaptureEventPanel () {

     setLayout (new GridLayout (2,1) );

     JPanel p = new JPanel ();
     p.setBackground (Color.LIGHT_GRAY);
     add (p);
     //Register to receive mouse events on the panel.
     p.addMouseListener (this);

     fTextOutput = new JTextArea ();
     fTextOutput.setEditable (false);
     add (fTextOutput);

  } // ctor

  // Implementation of Mouse Listener requires overriding
  // all five of its methods.


  public void mousePressed (MouseEvent e) {
    saySomething ("Mouse pressed; # of clicks: "
                  + e.getClickCount (), e);
  }

  public void mouseReleased (MouseEvent e) {
    saySomething ("Mouse released; # of clicks: "
                  + e.getClickCount (), e);
  }

  public void mouseEntered (MouseEvent e) {
    saySomething ("Mouse entered", e);
  }

  public void mouseExited (MouseEvent e) {
    saySomething ("Mouse exited", e);
  }

  public void mouseClicked (MouseEvent e) {
    saySomething ("Mouse clicked  (# of clicks: "
                  + e.getClickCount () + ")", e);
  }

  /** Put a message on text area that describes a mouse event.**/
  void saySomething (String event_description, MouseEvent e) {
     fTextOutput.insert (event_description + " detected on "
                         + e.getComponent ().getClass ().getName ()
                         + "." + "\n",0);
  }

} // class CaptureEventPanel

The getClass().getName() methods provide a way to find the class name of any instance of a Java Object subclass. Here we used it to find the identify of the component that generated the event.

MouseEvent

The MouseEvent class provides various methods to obtain information about the event. The inheritance heirarchy goes as:

java.lang.Object
   |
   +--java.util.EventObject
         |
         +--java.awt.AWTEvent
               |
               +--java.awt.event.ComponentEvent
                     |
                     +--java.awt.event.InputEvent
                           |
                           +--java.awt.event.MouseEvent

See the API Specifications for listings of the methods in these subclasses. A sampling of the methods include

  • getComponent() in ComponentEvent - used in the above example to find the Component that generated the event.
  • getY(),getX(),getPoint() in MouseEvent - provide coordinates of the mouse location.
  • getClickCount() in ActionEvent - number of times the mouse button clicked.

 

Latest update: Nov. 1, 2004

           Tech
Histogram UI
  Demo 1
Probablity Distrib.
  Demo 2 Demo 3
RejectionMethod
Histogram Stats
  Demo 4
Exercises

           Physics
Sim & Randomness
Custom Prob. Dist.
   Demo 1
Histogram Dist.
   Demo 2
Monte Carlo
  Demo 3
Exercises

  Part I Part II Part III
Java Core 1  2  3  4  5  6  7  8  9  10  11  12 13 14 15 16 17
18 19 20
21
22 23 24
Supplements

1  2  3  4  5  6  7  8  9  10  11  12

Tech 1  2  3  4  5  6  7  8  9  10  11  12
Physics 1  2  3  4  5  6  7  8  9  10  11  12

Java is a trademark of Sun Microsystems, Inc.