Java - Is it possible to override an ActionListener in the SuperClass?
Question
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?
Solution
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.
OTHER TIPS
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.
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.