Java Reference
In-Depth Information
6.1
The mouse listener
Interface MouseListener allows us to react to the use of the mouse buttons and to
entering or leaving the component to which the mouse listener is assigned. When-
ever the mouse enters or leaves the component or a mouse button is used inside
that component then the runtime system generates a MouseEvent and informs the
listener. Depending on the event, different methods of the listener are called. The
event object is passed as an argument to the method. The event object contains
information on the event; see Section 6.3 below. Interface MouseListener has five
such methods, all of which have to be implemented:
void mouseClicked(MouseEvent mevt)
void mouseEntered(MouseEvent mevt)
void mouseExited(MouseEvent mevt)
void mousePressed(MouseEvent mevt)
void mouseReleased(MouseEvent mevt)
mouseClicked(MouseEvent mevt) is automatically called by the runtime system
when a mouse button is clicked 2 .Tofind out which button was clicked one has
to look at the MouseEvent object mevt , see Section 6.3.
mouseEntered(MouseEvent mevt) is automatically called by the runtime system
if the mouse enters the component to which the listener is associated.
mouseExited(MouseEvent mevt) is automatically called by the runtime system
if the mouse leaves the component to which the listener is associated.
mousePressed(MouseEvent mevt) is automatically called by the runtime system
if a mouse button is pressed. To find out which button was pressed one has to
look at the MouseEvent object mevt , see Section 6.3.
mouseReleased(MouseEvent mevt) is automatically called by the runtime sys-
tem if a mouse button is released. To find out which button was released one
has to look at the MouseEvent object mevt , see Section 6.3.
A click results in calling three methods, mousePressed , mouseReleased and
mouseClicked .
6.2
The mouse motion listener
Interface MouseMotionListener supplies methods to track the mouse. If a Mouse-
MotionListener is assigned to a Swing component and the mouse is moved while
inside this component then the runtime system generates a MouseEvent and
2
In most operating systems 'clicking' is equivalent to 'press and release'.
Search WWH ::




Custom Search