Java Reference
In-Depth Information
Mouse Events
Mouse events are generated by the following types of user interaction:
A mouse click
n
A mouse entering a component's area
n
A mouse leaving a component's area
n
Any component can generate these events, which are implemented by a class through the
MouseListener
interface. This interface has five methods:
n
mouseClicked(
MouseEvent
)
n
mouseEntered(
MouseEvent
)
n
mouseExited(
MouseEvent
)
n
mousePressed(
MouseEvent
)
n
mouseReleased(
MouseEvent
)
Each takes the same basic form as
mouseReleased(
MouseEvent
)
:
public void mouseReleased(MouseEvent event) {
// ...
}
The following methods can be used on
MouseEvent
objects:
getClickCount()
—Returns the number of times the mouse was clicked as an
integer
n
12
getPoint()
—Returns the x,y coordinate within the component where the mouse
was clicked as a
Point
object
n
getX()
—Returns the x position
n
getY()
—Returns the y position
n
Mouse Motion Events
Mouse motion events occur when a mouse is moved over a component. As with other
mouse events, any component can generate mouse motion events. A class must imple-
ment the
MouseMotionListener
interface to support them.
There are two methods in the
MouseMotionListener
interface:
mouseDragged(
MouseEvent
)
and
mouseMoved(
MouseEvent
)
. They take the following forms: