Event Handling Programming in Java
Event Handling Programming in Java
MANAGEMENT
Lecture 14
1. Event Listener
2. Component
3. Event handler
4. Adapter Class
java.awt.dnd
javax.swing.event
2. Event Listener
An event listener is an object that wants to be notified when an event
has occured on a component.
The common interface that all event listener interfaces must extend
is EventListener.
To listen for ActionEvents the class must implement the interface
Example
Step one: Prepare to accept events
1. Declare an event handler class and specify that the class
extends a class that implements a certain GUI container.
For example:
c) Define constructor
Constructor is defined by declaring it and initializing Gui
components Within it.
Example
d) Create an object for the class
An object for the event handler class is created within main method.
using constructor of the class.
Example
2. . Implement an interface
Implementing an interface allows the class instance to listening the
events generated by a certain component
For example
The following statement shows a class named Eventhandling_App
That implements ActionListener interface
3. Define Event Handler
The appropriate abstract methods are implemented.
Example:
actionPerformed() method is implemented to give response
For example:
someComponent.addActionListener(instanceOfMyClass);
Event Handling Procedure
3. Create event handler method that that implements the
methods in listener interface.
For example: