When building GUI’s, the interface needs a way to listen and respond to events. Events are triggered when the user interacts with the GUI, such as pressing buttons and inserting text. One of these types of events in Java Swing is the ActionEvent. A commonly used component that generates a ActionEvent is a JButton, which is simply a button that produces an event when pressed. This article serves to explain three different ways an ActionListener can be added to a component.
Component as ActionListener
This is when the component itself listens for actions. To do this, simply implement the ActionListener interface in the component, for instance:
Components implementing the ActionListener therefore become both the source and listener. You would want to use this method if events are relatively simple and internal (e.g. not connecting multiple components).
This method of adding an ActionListener breaks the Single responsibility principle as the component is both responsible for creating events and handling it.
This method is when you use the ‘new’ keyword to create a new ActionListener for each component.
This implementation is great for simple listeners and it also separates the listener from the component. Avoid this if your trying to add ActionListeners to sub-components, for example, sub-components of a JPanel. This can result in messy code as every component is creating it’s own version of an ActionListener.
This technique requires you to create a separate class that implements ActionListener. A component that needs this ActionListener simply creates a new instance of this class and adds it.
This is the cleanest way to add an ActionListener. It separates the component from the listener, creates a reusable listener that can be used across different classes and allows for complex interactions between components.