Mouse events notify when the user uses the mouse (or similar input device) to interact with a component. Mouse events occur when the cursor enters or exits a component's onscreen area and when the user presses or releases one of the mouse buttons.
Tracking the cursor's motion involves significantly more system overhead than tracking other mouse events. That is why mouse-motion events are separated into Mouse Motion listener type (see How to Write a Mouse Motion Listener).
To track mouse wheel events, you can register a mouse-wheel listener. See How to Write a Mouse Wheel Listener for more information.
If
an application requires the detection of both mouse events and
mouse-motion events, use the MouseInputAdapter
class. This class implements the MouseInputListener,
a convenient interface that implements the MouseListener
and MouseMotionListener
interfaces. However, the MouseInputListener
interface does not implement the MouseWheelListener
interface.
Alternatively,
use the corresponding AWT MouseAdapter
class, which implements the MouseListener
,
MouseMotionListener
,
and MouseWheelListener
interfaces.
The
following example shows a mouse listener. At the top of the window
is a blank area (implemented by a class named BlankArea
).
The mouse listener listens for events both on the BlankArea
and on its container, an instance of MouseEventDemo
.
Each time a mouse event occurs, a descriptive message is displayed
under the blank area. By moving the cursor on top of the blank area
and occasionally pressing mouse buttons, you can fire mouse events.
Try this:
Click the Launch button to run MouseEventDemo using Java™ Web Start (download JDK 6). Alternatively, to compile and run the example yourself, consult the example index.
Move
the cursor into the yellow rectangle at the top of the window.
You
will see one or more mouse-entered events.
Press
and hold the left mouse button without moving the mouse.
You
will see a mouse-pressed event. You might see some extra mouse
events, such as mouse-exited and then mouse-entered.
Release
the mouse button.
You
will see a mouse-released event. If you did not move the mouse, a
mouse-clicked event will follow.
Press
and hold the mouse button again, and then drag the mouse so that
the cursor ends up outside the window. Release the mouse button.
You
will see a mouse-pressed event, followed by a mouse-exited event,
followed by a mouse-released event. You are not
notified of the cursor's motion. To get mouse-motion events, you
need to implement a mouse-motion
listener.
You can find the demo's code in MouseEventDemo.java and BlankArea.java. Here is the demo's mouse event handling code:
public class MouseEventDemo ... implements MouseListener { //where initialization occurs: //Register for mouse events on blankArea and the panel. blankArea.addMouseListener(this); addMouseListener(this); ... 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); } void saySomething(String eventDescription, MouseEvent e) { textArea.append(eventDescription + " detected on " + e.getComponent().getClass().getName() + "." + newline); } }
The MouseListener Interface
Method |
Purpose |
---|---|
Called just after the user clicks the listened-to component. |
|
Called just after the cursor enters the bounds of the listened-to component. |
|
Called just after the cursor exits the bounds of the listened-to component. |
|
Called just after the user presses a mouse button while the cursor is over the listened-to component. |
|
Called just after the user releases a mouse button after a mouse press over the listened-to component. |
The MouseAdapter class (the AWT adapter class) is abstract. All its methods have an empty body. So a developer can define methods for events specific to the application. You can also use the MouseInputAdapter class, which has all the methods available fromMouseListener
andMouseMotionListener
.
The MouseEvent Class
Method |
Purpose |
---|---|
Returns the number of quick, consecutive clicks the user has made (including this event). For example, returns 2 for a double click. |
|
Return the (x,y) position at which the event occurred, relative to the component that fired the event. |
|
int
getXOnScreen() |
Return the absolute (x,y) position of the event. These coordinates are relative to the virtual coordinate system for the multi-screen environment. Otherwise, these coordinates are relative to the coordinate system associated with the Component's Graphics Configuration. |
Returns
which mouse button, if any, has a changed state. One of the
following constants is returned: |
|
Returns
|
|
Returns
a |
The InputEvent Class
The
MouseEvent
class inherits many useful methods from InputEvent
and a couple handy methods from the ComponentEvent
and AWTEvent
classes.
Method |
Purpose |
|
---|---|---|
int
getID() |
Returns
the event type, which defines the particular action. For example,
the MouseEvent id reflects the state of the mouse buttons for
every mouse event. The following states could be specified by the
MouseEvent id: |
|
Component
getComponent() |
Returns
the component that fired the event. You can use this method
instead of the |
|
Returns the timestamp of when this event occurred. The higher the timestamp, the more recently the event occurred. |
||
boolean
isAltDown() |
Return the state of individual modifier keys at the time the event was fired. |
|
Returns
the state of all the modifier keys and mouse buttons when the
event was fired. You can use this method to determine which mouse
button was pressed (or released) when a mouse event was fired.
The (mouseEvent.getModifiers() & InputEvent.BUTTON3_MASK) == InputEvent.BUTTON3_MASK |
||
Returns
the extended modifier mask for this event. Extended modifiers
represent the state of the mouse button and all modal keys, such
as ALT, CTRL, META, just after the event occurred. You can check
the status of the modifiers using one of the following predefined
bitmasks: if (event.getModifiersEx() & (BUTTON1_DOWN_MASK | BUTTON2_DOWN_MASK | BUTTON3_DOWN_MASK) == BUTTON1_DOWN_MASK) { ... } Introduced in release 1.4. |
||
Returns a string describing the extended modifier keys and mouse buttons, such as "Shift", "Button1", or "Ctrl+Shift". These strings can be localized by changing the awt.properties file. Introduced in release 1.4. |
The MouseInfo Class
The
MouseInfo
class provides methods to obtain information about the mouse pointer
location at any time while an application runs.
Method |
Purpose |
---|---|
Returns
a |
|
Returns
the number of buttons on the mouse or |
Examples That
Use Mouse Listeners
The
following table lists the examples that use mouse listeners.
Example |
Where Described |
Notes |
---|---|---|
This section |
Reports all mouse events that occur within a blank panel to demonstrate the circumstances under which mouse events are fired. |
|
Uses
a subclass of |
||
Listens to mouse events on a table header. Sorts data in the selected column. |
||
Displays a popup menu in response to mouse clicks. |
||
The
custom component, |