Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
22 views

Event Handling Programming in Java

Uploaded by

evelynearbogusti
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views

Event Handling Programming in Java

Uploaded by

evelynearbogusti
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 22

FACULTY OF COMPUTING AND INFORMATION

MANAGEMENT

Data Analytics and Knowledge Engineering

Lecture 14

Diagnostic Analytics: Part 1


Event Handling
 Event-driven programming, is a programming methodology specify
how events are detected by a program and handled appropriately

 Event handling happens every time a user interacts with a user


interface.

 For example, when a user pushes a button, moves a mouse or types a


character.
Basic concepts
 1. Event Listener
Required components
The following parts are required for event handling

1. Event Listener
2. Component
3. Event handler
4. Adapter Class

2. Component to interact with user .


Example : a button

3. Event handler method for reponding to events


example
1. Event Listener
1. A class for implement a listener
Example :public class login implements ActionListener{}
1. Event Adapter
 Adapter classes is used to provide the default implementation of
listener interfaces
 It must be subclassed for it to be used.
 If adapter class is inherited then will not be forced to provide the
implementation of all the methods of listener interfaces. So it saves
code.
 The adapter classes are found in the following packages:
 java.awt.event,

 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

ActionListener, that in turn is an implementation of EventListener.


Example :
public class login implements ActionListener{}
3. Event Handler Method
 Event handler is an abstract method that is used for responding
to events detected by event listeners
 Example:
 The following method is automatically called whenever the user
clicks the button.

public void actionPerformed(ActionEvent e)


{..............
............
}
4. Component to interact with user .
 A component is an object having a graphical representation
 components are also called controls or widgets
 Examples:
Steps of Event Handling
There are three steps of event handling

1. Prepare to accept events

2. Implement an interface for listening the events

3. Define appropriate event handling method for responding to events


4. Register event listener on the component
1. Prepare to accept events
 Preparing to accept events consists of the following tasks
a) Import the classes that are necessary for handling events
b) Declare an event handler class
c) Declare a constructor and initialize GUI components
d) Create an object for the class using the constructor
Step one: Prepare to accept events
 (a) importing the necessary classes

 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

 When whenever the user clicks the button.


Event Handling Procedure
2. Register an instance of the event handler class as a listener on
one or more components.

For example:

someComponent.addActionListener(instanceOfMyClass);
Event Handling Procedure
3. Create event handler method that that implements the
methods in listener interface.

For example:

public void actionPerformed(ActionEvent e)

{ ...//code that reacts to the action... }


Sample Program
Sample Program(continued...)

You might also like