Chapter 22: Event Handling 647
boolean isPopupTrigger( )
Also available is thegetButton( )method, shown here:
int getButton( )
It returns a value that represents the button that caused the event. The return value will be
one of these constants defined byMouseEvent:
NOBUTTON BUTTON1 BUTTON2 BUTTON3
TheNOBUTTONvalue indicates that no button was pressed or released.
Java SE 6 added three methods toMouseEventthat obtain the coordinates of the mouse
relative to the screen rather than the component. They are shown here:
Point getLocationOnScreen( )
int getXOnScreen( )
int getYOnScreen( )
ThegetLocationOnScreen( )method returns aPointobject that contains both the X and
Y coordinate. The other two methods return the indicated coordinate.
The MouseWheelEvent Class
TheMouseWheelEventclass encapsulates a mouse wheel event. It is a subclass ofMouseEvent.
Not all mice have wheels. If a mouse has a wheel, it is located between the left and right
buttons. Mouse wheels are used for scrolling.MouseWheelEventdefines these two integer
constants:
WHEEL_BLOCK_SCROLL A page-up or page-down scroll event occurred.
WHEEL_UNIT_SCROLL A line-up or line-down scroll event occurred.
Here is one of the constructors defined byMouseWheelEvent:
MouseWheelEvent(Componentsrc, inttype, longwhen, intmodifiers,
intx, inty, intclicks, booleantriggersPopup,
intscrollHow, intamount, intcount)
Here,srcis a reference to the object that generated the event. The type of the event is specified
bytype.The system time at which the mouse event occurred is passed inwhen.Themodifiers
argument indicates which modifiers were pressed when the event occurred. The coordinates
of the mouse are passed inxandy.The number of clicks the wheel has rotated is passed in
clicks.ThetriggersPopupflag indicates if this event causes a pop-up menu to appear on this
platform. ThescrollHowvalue must be eitherWHEEL_UNIT_SCROLLorWHEELBLOCK
SCROLL. The number of units to scroll is passed inamount.Thecountparameter indicates
the number of rotational units that the wheel moved.