Search code examples
javaswinginheritanceactionlisteneranonymous-class

Changing jbutton action in subclass


I'm trying to change button action in a subclass because the form is pretty much exactly the except one asks for an ID. What I i tried doing was making a ActionListener object and instantiating it to an object of an anonymous class like so:

class ParentClass extends JPanel{
    JButton button;
    ActionListener buttonAction;

    ParentClass{
        button = new JButton("Parent Action");
        buttonAction = new ActionListener(){
            @Override
            public void actionPerformed(ActionEvent e) {
                System.out.println("The button was clicked by the parent class");
            }
        };
        button.add(buttonAction);
        add(button);
    }
}

    class ChildClass extends ParentClass{
        JButton button;
        ActionListener buttonAction;

        ChildClass{
            super();
            buttonAction = new ActionListener(){
                @Override
                public void actionPerformed(ActionEvent e) {
                    System.out.println("The button was clicked by the child class");
                }
            };
        }
    }


public static void main(String[] args){
    JFrame frame = new JFrame;
    frame.add(new ChildClass());
    frame.setSize(600, 500);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);

}

I was trying to use this method but the actionPerformed of buttonAction is never called. How can I make the button action different for the parent class and the subclass?


Solution

  • You can let parent class implement ActionListener, then use button.addActionListener(this) in order to add the action to button. Then in the subclass @Override actionPerformed method:

    class ParentClass extends JPanel implements ActionListener
    {
       ParentClass()
       {
           JButton button = new JButton("something");
           button.addActionListener(this);
           add(button);
        }
    
        @Override
        public void actionPerformed(ActionEvent event)
        {
            System.out.println("I am the parent.");
        }
    }
    
    class SubClass extends ParentClass
    {
        SubClass()
        {
           super();//initialize button
        }
    
         @Override
         public void actionPerformed(ActionEvent event)
         {
             System.out.println("I am the child.");
         }
    }
    

    Another way is to add the ActionListener and inside it, only call a method. Something like buttonPressed. Then in subclass @Override buttonPressed method.

    A complete example:

    public class Test extends JFrame {
        public Test() {
            super("test");
            setDefaultCloseOperation(EXIT_ON_CLOSE);
    
            setLayout(new GridLayout(2, 1));
            add(new ParentPanel());
            add(new ChildPanel());
            pack();
            setLocationByPlatform(true);
        }
    
        private class ParentPanel extends JPanel implements ActionListener {
            public ParentPanel() {
                super(new BorderLayout());
    
                JButton button = new JButton("My Class:" + getClass());
                button.addActionListener(this);
                add(button);
            }
    
            @Override
            public void actionPerformed(ActionEvent e) {
                System.out.println("Parent");
    
            }
        }
    
        private class ChildPanel extends ParentPanel {
            public ChildPanel() {
                super();
            }
    
            @Override
            public void actionPerformed(ActionEvent e) {
                System.out.println("Child");
            }
        }
    
        public static void main(String[] args) {
            SwingUtilities.invokeLater(() -> new Test().setVisible(true));
        }
    }