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