
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Differences Between Event Listener Interface and Event Adapter Class in Java
In Java, the EventListener interface defines the methods that must be implemented by an event handler for a particular kind of event, whereas an Event Adapter class provides a default implementation of the EventListener interface.
Java EventListener
The EventListeners are the backbone of every component to handle the events. Every method of a particular EventListener will have a single parameter as an instance, which is a subclass of the EventObject class. An EventListener interface needs to be extended, and it will be defined in java.util package.
Syntax
The following is the syntax for EventListener declaration:
public interface EventListener
EventListener interfaces
The following are EventListener interfaces in Java:
ActionListener
The ActionListener method handles action events, typically from buttons, menu items, etc. When the action event occurs, the actionPerformed method of that object is invoked.
Method Declaration:
public interface ActionListener extends EventListener
KeyListener
The KeyListener method handles keyboard input events. The relevant method in the listener object is then invoked, and the KeyEvent is passed.
Method Declaration:
public interface KeyListener extends EventListener
MouseListener
The MouseListener method handles mouse events on components. The listener object created from that class is then registered with a component using the component's addMouseListener method.
Method Declaration:
public interface MouseListener extends EventListener
FocusListener
When the component gains or loses keyboard focus, the FocusListener method in the listener object is invoked, and the FocusEvent is passed to it.
Method Declaration:
public interface FocusListener extends EventListener
ItemListener
The ItemListener method handles state changes in items like checkboxes, radio buttons, etc. When an item-selection event occurs, the listener object's itemStateChanged method is invoked.
Method Declaration:
public interface ItemListener extends EventListener
When to Use EventListener
We can use the EventListener Interfaces in the following cases:
- When we need to implement all or most of the methods in the interface.
- When extending another class, and can't extend the adapter.
- In the implementation of multiple listener interfaces.
- For the Lambda expressions for single-method interfaces.
Example of EventListener
Below is an example of the EventListener interface on a Swing GUI in Java:
import java.awt.*; import java.awt.event.*; import javax.swing.*; public class KeyListenerTest implements KeyListener, ActionListener { JFrame frame; JTextField tf; JLabel lbl; JButton btn; public KeyListenerTest() { frame = new JFrame(); lbl = new JLabel(); tf = new JTextField(15); tf.addKeyListener(this); btn = new JButton("Clear"); btn.addActionListener(this); JPanel panel = new JPanel(); panel.add(tf); panel.add(btn); frame.setLayout(new BorderLayout()); frame.add(lbl, BorderLayout.NORTH); frame.add(panel, BorderLayout.SOUTH); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setLocationRelativeTo(null); frame.setSize(300, 200); frame.setVisible(true); } @Override public void keyTyped(KeyEvent ke) { lbl.setText("You have typed "+ ke.getKeyChar()); } @Override public void keyPressed(KeyEvent ke) { lbl.setText("You have pressed "+ ke.getKeyChar()); } @Override public void keyReleased(KeyEvent ke) { lbl.setText("You have released "+ ke.getKeyChar()); } @Override public void actionPerformed(ActionEvent ae) { tf.setText(""); } public static void main(String args[]) { new KeyListenerTest(); } }
Output
Java Event Adapter
The Abstract classes can be called as Event Adapter for receiving various events. An Event Adapter class gives a default implementation of all the methods in the EventListener interface.
Event Adapter Classes
The following are a few Event Adapter classes in Java:
FocusAdapter
The FocusAdapter class is an abstract (adapter) class for receiving events of keyboard focus events. All methods of this class are void.
Method Declaration:
public abstract class FocusAdapter extends Object implements FocusListener
KeyAdapter
The KeyAdapter class is an abstract (adapter) class to receive keyboard events. All its methods are empty.
Method Declaration:
public abstract class KeyAdapter extends Object implements KeyListener
MouseAdapter
The MouseAdapter class is an abstract (adapter) class for getting mouse events. All the methods of this class are null.
Method Declaration:
public abstract class MouseAdapter extends Object implements MouseListener, MouseWheelListener, MouseMotionListener
WindowAdapter
The WindowAdapter class is an abstract (adapter) class to receive window events. All its methods are empty.
Method Declaration:
public abstract class WindowAdapter extends Object implements WindowListener, WindowStateListener, WindowFocusListener
When to Use Event Adapter
We can use the Event Adapter Classes in the following cases:
- Only needed when we want to implement one or two methods from the interface.
- When we want to reduce boilerplate code.
- Readability and simplicity are priorities.
Example of Event Adapter
Below is an example of the Event Adapter classes on a Swing GUI in Java:
import java.awt.*; import java.awt.event.*; import javax.swing.*; public class KeyAdapterTest { private JFrame frame; private JLabel headLabel; private JLabel msgLabel; private JPanel controlPanel; public KeyAdapterTest() { initializeUI(); } private void initializeUI() { frame = new JFrame("KeyAdapter class"); frame.setSize(350, 275); frame.setLayout(new GridLayout(3, 1)); headLabel = new JLabel("", JLabel.CENTER); msgLabel = new JLabel("", JLabel.CENTER); msgLabel.setSize(300, 100); frame.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent windowEvent) { System.exit(0); } }); controlPanel = new JPanel(); controlPanel.setLayout(new FlowLayout()); frame.add(headLabel); frame.add(controlPanel); frame.add(msgLabel); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setLocationRelativeTo(null); frame.setVisible(true); } private void showMouseApapter() { headLabel.setText("KeyAdapter Test"); final JTextField textField = new JTextField(10); JButton displayButton = new JButton("Display"); displayButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { msgLabel.setText("You entered : " + textField.getText()); } }); textField.addKeyListener(new KeyAdapter() { public void keyPressed(KeyEvent e) { if (e.getKeyCode() == KeyEvent.VK_ENTER) { msgLabel.setText("You entered : " + textField.getText()); } } }); controlPanel.add(textField); controlPanel.add(displayButton); frame.setVisible(true); } public static void main(String[] args) { KeyAdapterTest test = new KeyAdapterTest(); test.showMouseApapter(); } }
Output
Differences Between Event Listener Interface and Event Adapter
The following are the key differences between an event listener interface and an event adapter class in Java:
Criteria | Event Listener Interface | Event Adapter Class |
---|---|---|
Type | Interface | Abstract Class |
Method Implementations | None (all abstract) | Empty implementations |
Implementation Requirement | Must implement all methods | Can override only the needed methods |
Flexibility | More flexible | Slightly less flexible |
Inheritance | Multiple interfaces possible | Single inheritance only |