• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 370
  • Last Modified:

Action listeners

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
sewalsh
Asked:
sewalsh
  • 8
  • 5
  • 3
  • +4
1 Solution
 
CEHJCommented:
Mouse motion requires its own type of listener. See:

http://javaalmanac.com/egs/java.awt.event/MouseMotion.html
0
 
CEHJCommented:
You could also make the class implement MouseMotionListener to avoid using that extra class if you want
0
 
Mayank SAssociate Director - Product EngineeringCommented:
>> 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
sewalshAuthor Commented:
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
 
Mayank SAssociate Director - Product EngineeringCommented:
You can simply add it as an anonymous listener.

Mayank.
0
 
CEHJCommented:
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
 
Mayank SAssociate Director - Product EngineeringCommented:
He he... CEHJ, that's exactly the second method which I posted in my first comment :-)

Mayank.
0
 
CEHJCommented:
Oh - didn't notice
0
 
sewalshAuthor Commented:
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
 
Mayank SAssociate Director - Product EngineeringCommented:
imported java.awt.event.* ??
0
 
sewalshAuthor Commented:
already imported mayankeagle
0
 
Mayank SAssociate Director - Product EngineeringCommented:
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
 
CEHJCommented:
>>extends Dimension

You won't get events that way. Extend Canvas.
0
 
lolottCommented:
I just want to tell that java.awt.Rectangle has no add'Any'Listener methods. Maybe You should try Canvas!
0
 
kokchoon78Commented:
Rectangle is not extended from Component Class, so there are not listener methods.

0
 
Mayank SAssociate Director - Product EngineeringCommented:
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
 
CleanupPingCommented:
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
 
jimmackCommented:
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
 
Mayank SAssociate Director - Product EngineeringCommented:
Please award some points to CEHJ also.
0
 
jimmackCommented:
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
 
Mayank SAssociate Director - Product EngineeringCommented:
All right, as you wish, Jim :-)
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 8
  • 5
  • 3
  • +4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now