File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes Action Listeners Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Action Listeners" Watch "Action Listeners" New topic

Action Listeners

Ian glasgow

Joined: Jan 29, 2004
Posts: 8
How do I set two different action listeners in two different classes to listen for a button click in just one instance of a class?
Joe Ess

Joined: Oct 29, 2001
Posts: 9189

[How To Ask Questions On JavaRanch]
Ian glasgow

Joined: Jan 29, 2004
Posts: 8
Do I not have to specifiy which class the button is in?
Eg class1 listens for the button click
class2 also listens for it, where the button actually is
in class2 I put:
public void actionPerformed(ActionEvent ae)
// then do this
but what would I put in class one where the button is not instantiated?
surely not the same??
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791

In this little chunk of code, "button1" is a variable name. You can use this variable name just as above in Class1 and Class2 - assuming it is visible given current scope. Maybe that's your question - can you "see" button1 from Class1? If not, we might want to get a bit more advanced with your overall design.

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Ian glasgow

Joined: Jan 29, 2004
Posts: 8
Yeah, that's what I was getting do i "see" one button in several different classes.
any suggestions?
Maulin Vasavada
Ranch Hand

Joined: Nov 04, 2001
Posts: 1873
Hi Ian
As Stan James suggested, we might want to go more advanced with your overall design.
Here is what I guess you have,
1. You have Class1 and Class2. Both of them are some UI things.
2. Class2 is having a button in addition to some other things.
3. As you want to catch button click in Class2 you are making it
implements ActionListener where you override actionPerformed() method
4. Now, you also want Class1 to be a listener to this button click.
5. You have Class1 also implementing ActionListener
Here are two possible solutions popup from top of my mind,
- You can make object of Class1 available in Class2 via some constructor
or some method
- Then in the Class2 code you can write similar to what Joe suggested,
button1.addActionListener(class1Object); // where class1Object is a reference of Class1 type object you might be creating somewhere
button1.addActionListener(this); // for Class2 itself
This would produce dependency of Class2 on Class1 as we have to create a reference in Class2. So, if you want Class2 to be independent and still make things work the way you want then you probably can with the below second option,
- Make the button firePropertyChange() event
- Make Class1 dependent upon Class2 but having a reference to Class2
- Put a method in Class2 to return a reference to the button in question
- Use Class2's reference in Class1 as,
This second one is complex if you are not aware of this property change things but I am sure you can google things and find out some little tutorial and example about how it all works..
3. the last thing would be to explore if Observer/Observable pattern works for you which is again a complex in comparison with 1st option above and works very similarly to 2nd ...
I agree. Here's the link:
subject: Action Listeners
jQuery in Action, 3rd edition