views:

112

answers:

3

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?

+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
Ahh i'll try it this way, let you know if it works
KP65
+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
like the nice chap mentioned above?
KP65
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