Listener vs Adapter: Explanation, Importance, Replacement etc. are discussed.
1. What is an event?
Whenever the user interacts with a component, the component generates an event. Here component may be a window frame, button, text field or a mouse click. The interaction by the user with the component may be in the form of clicking a button, pressing enter key in the text field or typing in a text area etc.
Generating an event (with the user’s interaction) is the internal behavior of the component architectured by the Java Designers.
2. How many types of events exist?
Basically, two types – Semantic events and low-level events. Semantic events are generated by the software components like frame, button etc. and low-level are generated by hardware components like mouse and keyboard etc.
3. What is action event or item event or mouse event? Why different names for events?
To differentiate the events and to give different actions for different components, different names are given for actions generated by components. All events are defined as Java classes. For example, the event generated by the button is known as ActionEvent and the event generated by the frame is known as WindowEvent and similarly with checkbox, it is known as ItemEvent etc.
4. Then, what is listener?
To manage (or handle) the event generated by a component, another interface is designed known as listener. Action, event and listener, all the three are very different. Do not get confused.
- Action: What user does is known as action. Example, a click over button. Here, click is the action performed by the user.
- Event: The action done by the component when the user’s action takes place is known as event. That is, event is generated (not seen, it is software) against action.
- Listener: It is an interface that handles the event. That is, the event is caught by the listener and when caught, immediately executes some method filled up with code. Other way, the method called gives life to the user action.
- Action by user: Mouse click over a button
- Event generated: ActionEvnet
- Listener that handles ActionEvent: ActionListener
- Method called implicitly: actionPerformed() method.
Like each event got a name, even the listeners got different names – ActionListener, WindowListener etc.
For more clarity,
The code of actionPerformed() contains the actions of what is to be done (like user name and password validation etc.) when the user clicks a button.
5. What is the problem with listener interfaces?
Actually problem does not occur with every listener, but occurs with a few. A few listeners contain more than one abstract method. For example, WindowListener, used for frame closing, comes with 7 abstract methods. We are interested in only one abstract method to close the frame, but being WindowListener is an interface, we are forced to override remaining 6 methods also, just atleast with empty body. This is the only problem, else fine. Some listeners like ActionListener comes with only one abstract method and with them no problem at all.
6. Many say Java is simple and why designers made listeners like this?
7. What is adapter?
Designers too felt the problem with listeners and to overcome introduced adapters in JDK 1.1. Adapters are replacement to listeners. The advantage with adapter is we can override any number of methods we would like and not all. For example, if we use WindowAdapter (instead of WindowListener), we can override only one method to close the frame.
Adapters make event handling simple. Any listener has more than one abstract method has got a corresponding adapter class. For example, MouseListener with 5 abstract methods has got a corresponding adapter known as MouseAdapter. But ActionListener and ItemListener do not have corresponding adapter class as they contain only one abstract method.
Adapters are abstract classes introduced from JDK 1.1.
8. What is the list of most commonly used listeners and adapters?
Following table gives some important listeners and their corresponding adapter classes. (Listener vs Adapter)
|Listener Interface||Corresponding Adapter|
The values in the parentheses indicate the abstract methods available in the listener interface.
Pass your comments for improvement on this tutorial Listener vs Adapter.
2 thoughts on “Listener vs Adapter”
good and simply answer
Awsome site.. Its really vrrrrrrry helpful.. Before reading this i was confused between listener and adapter bt now i completely understood the difference..