Event Handling Methodologies
This tutorial is on “Various ways to apply event handling in your desktop application”.
1. Event Listener and Event Source in the same class
This is same as what we did in previous tutorial on “ActionListener – Click event handling“.
Event Listener is implemented in the class where we have components like Button, Label, etc. In this implementation, we have to add all the methods of Listener in the same class.
Characteristics:
- Components/Event Sources can be easily accessed from Listener methods
- Event can be applied to any component by passing current object(this). No need of creating other object.
- View as well as Logic is combined in single class which is a bad practice.
- All the methods must be defined. Blank definition is to be given even if we don’t want to use it.
2. Event Listener and Event Source in the different class
Event Listener is implemented in the different class than where we have components. In this case to apply the listener, we have to pass the object of that class. so that whenever event occurs JRE can call the method of Listener.
Characteristics:
- Components/Event Sources cannot be easily accessed from Listener methods. We must have object of base class(where we have components) in the Listener class(where we have Listener methods)
- Event can be applied to a component by passing object of newly created Listener class.
- View and Controller part can be separated which is a good practice.
- All the methods must be defined in our newly created class. Blank definition is to be given even if we don’t want to use it.
3. Anonymous Listener Interface
Anonymous Listener can be passed in the argument of addXYZListner() method. This anonymous class must consist of all the method.
Characteristics:
- Components/Event Sources can be easily accessed from Listener methods
- To apply an event we have to pass anonymous object of Listener Interface
- Logic is to be written in complex inner methods
- All the methods must be defined. Blank definition is to be given even if we don’t want to use it.
4. Anonymous Adapter Class
An Adapter class is provided in java.awt.event package for those Listener interfaces which are having more than one method. Such as MouseAdapter, WindowAdapter, etc. Adapter class is derived from Listener interface and it provides blank definition of all the methods of derived listener. So that when we use Adapter class instead of Listener interface we can skip writing any of the method which are unused.
Characteristics:
- Components/Event Sources can be easily accessed from Listener methods
- To apply an event we have to pass anonymous object of Adapter class
- Logic is to be written in complex inner methods
- Not compulsory to define all the methods. No need to write Blank definition if not required.
5. Extending Adapter Class
As we all know that: Java do not support multiple inheritance we cannot derive our Applet/Frame/JFrame based classes by any other class at the same time. In that case what can be done is: we can create a new class and that class will be derived from Adapter class. And our newly derived Adapter class can be used as an argument of addXYZListener() method.
Characteristics:
- Components/Event Sources cannot be easily accessed from Listener methods. We must have object of base class(where we have components) in the Listener class(where we have Listener methods)
- Event can be applied to a component by passing object of newly created adapter class which extends built in adapter class.
- View and Controller part can be separated which is a good practice.
- Not compulsory to define all the methods. No need to write Blank definition if not required.
Recent Comments