• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Action Listeners

 
Ian glasgow
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Bartender
Pie
Posts: 9266
10
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Ian glasgow
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
method:
public void actionPerformed(ActionEvent ae)
{
if(ae.getSource()==button1)
{
// 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
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.
 
Ian glasgow
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah, that's what I was getting at..how do i "see" one button in several different classes.
any suggestions?
Ta..
Ian
 
Maulin Vasavada
Ranch Hand
Posts: 1873
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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,
1.
- 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,
2.
- 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,
class2Object.getTheButton().addPropertyChangeListener(this)...
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 ...
hth,
Regards
Maulin
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic