You don't invoke an actionListener directly. It is done for you. Here is an example.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ActionListenerDemo implements ActionListener {
JFrame frame = new JFrame("ActionLIstener Demo");
JPanel panel = new JPanel();
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> new ActionListenerDemo());
}
public ActionListenerDemo() {
panel.setPreferredSize(new Dimension(400, 200));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(panel);
JButton button = new JButton("Button1");
// listener using method - supported from the beginning.
button.addActionListener(this);
// listener using lambdas - supported in java 8+
button.addActionListener(
(ae) -> System.out.println("Button pressed - handled from lambda"));
panel.add(button);
frame.pack();
frame.setLocationRelativeTo(null); // center on screen
frame.setVisible(true);
}
public void actionPerformed(ActionEvent ae) {
System.out.println("Button pressed - handled from method");
}
}
The code that goes in the listener is what you want the button to do.
Regards,
Jim