If have two classes, Class A
and Class B
, B is a subclass of A...if my Class A(the superclass) has a JButton
with an ActionListener
which is implemented by an anonymous inner class, how can I override what the button does in the subclass?
views:
112answers:
3
+1
A:
I'm not really sure how your code looks like, but here is a rather generic "solution" (not tested):
for (ActionListener al : super.getThatButton().getActionListeners())
{
super.getThatButton().removeActionListener(al);
}
And add a new ActionListener afterwards. I think that's what you might be looking for, but I'm not sure. I'd just add another ActionListener or make it use Actions instead of ActionListeners.
Tedil
2010-03-13 18:00:31
Ahh i'll try it this way, let you know if it works
KP65
2010-03-13 18:39:37
+1
A:
Your only option is to remove the current ActionListener
and add a new one. You can't extend an anonymous inner class by definition: it's anonymous.
cletus
2010-03-13 18:00:37
A:
Hmm, you could have the listener call a protected method of some sort:
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
doStuff();
}
});
Then you can override doStuff
in the subclass. This seems simpler than mucking about with the events more than you have to.
Tikhon Jelvis
2010-03-13 19:41:59