Link to home
Start Free TrialLog in
Avatar of taborrg
taborrg

asked on

Use mouseListener to trigger an event when the mouse is released.

Hi,

I'm trying to use mouseListener to trigger an event when the mouse is released.

Do I need a loop for this?

Thanks
Avatar of for_yan
for_yan
Flag of United States of America image

No you don't need a loop

Thee is amethod
public void mouseRelaesed(MouseEvent me) {


}

this method will be called when mouse is relead
no need to loop, just override the "public void mouseReleased(MouseEvent e)" method of mouse listener
look here for an example how to write mouse listener:
http://www.iam.ubc.ca/guides/javatut99/uiswing/events/mouselistener.html
Avatar of taborrg
taborrg

ASKER

for_yan - How do I make use of that in a method?

Thanks
You add MouseListener to some component and when
mosue is realesed on that component -say button or panel this mehod will be automatically called
then you put any activity which you want inside that methoid and it wil be executed:

  public void mouseReleased(MouseEvent e) {
 
       saySomething("Mouse released; # of clicks: "
                    + e.getClickCount(), e);
    }
Avatar of taborrg

ASKER

for_yan

So each method in a class needs to have its own mouseListener?

If I have one in a class, can any method in that class make use of it?

Thanks
ASKER CERTIFIED SOLUTION
Avatar of for_yan
for_yan
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
No, your class implements MouseListener - it means you need only one method mouseRelaesed in a c lass, like in my example
Avatar of taborrg

ASKER

Great!  Thanks for the help.
Then you cann addMouseListener(this)

to any number of components
and then when you get into method
you may check which of the com;omenets
caused the even - you just say

if(me.getSource().equals(panle) {
//do somethingh
//return

}
if(me.getSource().equals(button){
//do something else

}