Java Is it possible to override ActionListener in SuperClass?

If there are two classes: Class Aand Class B, B is a subclass of A ... if my class A (superclass) has JButtonc ActionListener, which is implemented by an anonymous inner class, how can I redefine what the button does in the subclass?

+3
source share
3 answers

Hmm, you could call the listener as a protected method:

    button.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent arg0) {
            doStuff();
        }
    });

Then you can override doStuffin a subclass. It seems easier than spoofing events more than you need.

+3
source

I'm not sure what your code looks like, but here is a pretty general โ€œsolutionโ€ (not tested):

for (ActionListener al : super.getThatButton().getActionListeners())
{
    super.getThatButton().removeActionListener(al);
}

ActionListener. , , , , . ActionListener Actions ActionListeners.

+1

Your only option is to delete the current one ActionListenerand add a new one. You cannot extend an anonymous inner class by definition: it is anonymous.

+1
source

Source: https://habr.com/ru/post/1736808/


All Articles