Java Reference
In-Depth Information
informs the listener. Depending on whether the mouse was moved or dragged 3 ,
different methods of the listener are called. The event object is passed as an
argument to the method. A MouseMotionListener has two methods; both of which
have to be implemented:
void mouseMoved(MouseEvent mevt)
void mouseDragged(MouseEvent mevt)
mouseMoved(MouseEvent mevt) is automatically called if the mouse is moved
while it is in a component that the listener is associated to. The new mouse
position can be found by analysing the event object mevt . The mouse is 'moved'
if the runtime system receives a signal from the mouse. Usually this results in
a change of the position of the mouse pointer on the screen by at least one
pixel.
mouseDragged(MouseEvent mevt) is automatically called if the mouse is moved
while a mouse button is pressed.
6.3
Mouse events
The methods described above all receive a ( MouseEvent )asaparameter. This
object contains information on what triggered the listener. The following meth-
ods from the class MouseEvent show where the event occurred. The x - and y -
coordinates are in pixels in the coordinate system of the component to which the
listener is assigned:
int getX()
int getY()
To find out which button was used we can use the following methods from class
SwingUtilities . They return true if the button appearing in the method name
is used, and false otherwise:
boolean SwingUtilities.isLeftMouseButton(MouseEvent me)
boolean SwingUtilities.isMiddleMouseButton(MouseEvent me)
boolean SwingUtilities.isRightMouseButton(MouseEvent me)
Java does not (yet) support double clicks . One can use method
int getClickCount()
to determine the number of clicks in a short period. The length of this period de-
pends on the platform and can usually be set using utilities of the operating system.
Then a double click can be checked by using if(mevt.getClickCount() == 2) .
3
Dragging means moving the mouse with a button pressed.
Search WWH ::




Custom Search