Online Software Educational Training - IT Tutorials - Online Education Training for Computer Software


Home

JAVA APPLET

HANDLING  MOUSE EVENTS

The MouseListener interface and its corresponding adapter class, MouseAdapter, contain these methods: 

void mouseClicked(MouseEvent) 

Called just after the user clicks the listened-to component. 

void mouseEntered(MouseEvent) 

Called just after the cursor enters the bounds of the listened-to component. 

void mouseExited(MouseEvent) 

Called just after the cursor exits the bounds of the listened-to component. 

void mousePressed(MouseEvent) 

Called just after the user presses a mouse button while the cursor is over the listened-to component. 

void mouseReleased(MouseEvent) 

Called just after the user releases a mouse button after a mouse press over the listened-to component. 
One complication affects mouse-entered, mouse-exited, and mouse-released events. When the user drags (presses and holds the mouse button and then moves the mouse), then the component that the cursor was over when the drag started is the one that receives all subsequent mouse and mouse-motion events up to and including the mouse button release. That means that no other component will receive a single mouse event -- not even a mouse-released event -- while the drag is occurring. 

Each mouse event method has a single parameter: a MouseEvent object. The MouseEvent class defines the following useful methods: 

int getClickCount() 

Returns the number of quick, consecutive clicks the user has made (including this event). For example, returns 2 for a double click. 

int getX() 
int getY() 

Point getPoint() 

Return the (x,y) position at which the event occurred, relative to the component that fired the event. 

boolean isPopupTrigger() 

Returns true if the mouse event should cause a popup menu to appear. Because popup triggers are platform dependent, if your program uses popup menus, you should call isPopupTrigger for all mouse-pressed and mouse-released events fired by components over which the popup can appear.  

An example using mouse events






Previous

Next