Search code examples
javaactionlisteners

Java - Is it possible to override an ActionListener in the SuperClass?


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.