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
|