?
Solved

Action listeners

Posted on 2003-03-27
23
Medium Priority
?
368 Views
Last Modified: 2010-03-31
How does one implement more than one actionlistener in the same class. I have actionlisteners and i'm trying to introduce a mouse motion listener but to no success. Any ideas?
0
Comment
Question by:sewalsh
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 5
  • 3
  • +4
23 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 8217193
Mouse motion requires its own type of listener. See:

http://javaalmanac.com/egs/java.awt.event/MouseMotion.html
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 8217207
You could also make the class implement MouseMotionListener to avoid using that extra class if you want
0
 
LVL 30

Accepted Solution

by:
Mayank S earned 160 total points
ID: 8217212
>> more than one actionlistener in the same class

One class can only have one actionPerformed () method. How-ever, it can do various things based on the source of the ActionEvent, as:

button1.addActionListener ( this ) ;
button2.addActionListener ( this ) ;
..
..

public void actionPerformed ( ActionEvent ae )
{
  if ( ae.getSource () == button1 )
  {
    // do something

  } // end if

  else if ( ae.getSource () == button2 )
  {
    // do something

  } // end if

} // end of actionPerformed ()


Or else, you can have different classes that implement the ActionListener interface and do:

button1.addActionListener ( class1 ) ;
button2.addActionListener ( class2 ) ;

where class1, class2 implement the ActionListener interface and take care of ActionEvents generated by button1 and button2 respectively.

Or else, you can also have anonymous inner classes like:

button1.addActionListener ( new ActionAdapter () {
  public void actionPerformed ( ActionEvent ae )
  {
    // do something

  } // end of actionPerformed ()
} ) ;


Is that what you want?

Mayank.
0
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 

Author Comment

by:sewalsh
ID: 8217236
hi, i need to implement two Listeners in the same class. am i going to have to write another class (implementing other listener i want) and then create an new instance of that class in my other class?
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 8217268
You can simply add it as an anonymous listener.

Mayank.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 8217278
They can just use the same listener. Distinguish as follows:

public void actionPerformed (ActionEvent e) {
  if (e.getSource() == first) {
    // do something
  }
  else if (e.getSource() == hasMouseListenerToo) {
    // do something else
  }
}
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 8217298
He he... CEHJ, that's exactly the second method which I posted in my first comment :-)

Mayank.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 8217317
Oh - didn't notice
0
 

Author Comment

by:sewalsh
ID: 8217327
hi again. i wrote another class that uses a mouse motion listener. In my other class i create an instance of this new class and initialise it with a rectangle. however, when i try to add the mouse motion listener it gives me an error. here's is the code:

class mouseMotion extends Dimension implements MouseMotionListener {
    Rectangle rect;
    public mouseMotion(Rectangle re) {
        rect = re;
        rect.addMouseMotionListener(this);
    }


   public void mouseMoved(MouseEvent e){
     System.out.println("mouse moved within!");
   }
   
   public void mouseDragged(MouseEvent e) {
     //do something
    }

}

and error:

editapplet.java:530: cannot resolve symbol
symbol  : method addMouseMotionListener  (editapplet.mouseMotion)
location: class java.awt.Rectangle
        re.addMouseMotionListener(this);
          ^
1 error
bash-2.04$
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 8217345
imported java.awt.event.* ??
0
 

Author Comment

by:sewalsh
ID: 8217357
already imported mayankeagle
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 8217409
Don't think that you'll be able add a mouse-motion-listener only for the rectangle that way. Try adding it to the entire Frame/ Applet in its constructor.

Mayank.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 8217489
>>extends Dimension

You won't get events that way. Extend Canvas.
0
 

Expert Comment

by:lolott
ID: 8217546
I just want to tell that java.awt.Rectangle has no add'Any'Listener methods. Maybe You should try Canvas!
0
 
LVL 4

Expert Comment

by:kokchoon78
ID: 8223353
Rectangle is not extended from Component Class, so there are not listener methods.

0
 
LVL 30

Expert Comment

by:Mayank S
ID: 8223457
sewalsh,

I think that originally in your question, you meant how to add more than one listener, not more than one action-listener. Its a little evident from your code, that you probably want a class to implement ActionListener as well as MouseMotionListener.

Well, IF that is the case, then you can happily proceed with:


public class MyClass extends Applet
  implements ActionListener, MouseMotionListener
{
  Rectangle rect ;
  ..
  ..

  public void init ()
  {
    button1.addActionListener ( this ) ;
    ..
    ..
    addMouseMotionListener ( this ) ;

  } // end of init ()

  public void paint ( Graphics g )
  {
    ..

  } // end of paint ()

  public void actionPerformed ( ActionEvent ae )
  {
    ..

  } // end of actionPerformed ()

  public void mouseMoved ( MouseEvent me )
  {
    ..

  } // end of mouseMoved ()

  public void mouseDragged ( MouseEvent me )
  {
    ..

  } // end of mouseDragged ()

} // class definition over


I think this should fit into your requirements. Because a class can extend only one super-class, but it can implement as many interfaces as it wants.

Mayank.
 
0
 

Expert Comment

by:CleanupPing
ID: 9058458
sewalsh:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
EXPERTS:
Post your closing recommendations!  No comment means you don't care.
0
 
LVL 15

Expert Comment

by:jimmack
ID: 9734788
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

Accept mayankeagle's comment as answer.

Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

jimmack
EE Cleanup Volunteer
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 9738258
Please award some points to CEHJ also.
0
 
LVL 15

Expert Comment

by:jimmack
ID: 9738310
I think I'll stick by my decision on this one.  When I read the question, I was expecting someone to respond with both interfaces implemented by the class (as the questioner requested).  You were (eventually ;-)) the only one who explained this in a way that the questioner seemed to understand.  CEHJ did correctly identify this in his second comment, but the questioner did not understand.
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 9738940
All right, as you wish, Jim :-)
0

Featured Post

Want to be a Web Developer? Get Certified Today!

Enroll in the Certified Web Development Professional course package to learn HTML, Javascript, and PHP. Build a solid foundation to work toward your dream job!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month13 days, 10 hours left to enroll

800 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question