2. Why Does a Program or Application Need to
be Event Driven?
Before event handling came into the picture, a program had to collect all the user
information itself to know what it was doing at a given point in time.
This means that after being run or initialized, a program was always in a big repeat loop that
was waiting for the user to do something.
So, the program was looking for any action – from the press of a button to slider movement.
After it came to know that something has happened from the user’s side, it prepared itself to
deliver the appropriate response. This is referred to as polling.
Although polling gets the job done, more often than not, it comes across as too
unmanageable and time-consuming a task.
3. If we consider using it for modern-day applications, it doesn’t really fit the
requirements. Two primary reasons make polling unsuitable for modern
applications –
1.Polling puts all the code inside the big repeat loop, and the interactions
that take place inside this location are too complex.
2.Also, polling makes a program enter a never-ending loop, which results in the
exhaustion of CPU cycles without any guarantee of action coming from the user.
The Abstract Window Toolkit or AWT has gone ahead and struck
association with a different working model for solving the issue discussed
above. This new model is event-driven programming.
.
4. With AWT, there is no need for the program to look out for user-
generated events. It is the Java run time that does this job. It
intimates the program as soon as an event takes place. It saves a
valuable resource from exhaustion and handles user interaction
better.
Abstract Window Toolkit (AWT) is a set of application
program interfaces used by Java programmers to create
graphical user interface ( GUI ) objects, such as buttons, scroll
bars, and windows
5. Event Handling
Event Handling is the mechanism that controls the event &
decides what should happen if an event occurs.
Chain of Responsibility
Delegation Event Model
6. In the old days, Java used a Chain of Responsibility pattern
to process events.
For example, when a button is clicked, a event is
generated, which then is passed through a chain of
components.
The chain of components is defined by the hierarchy of
classes and interfaces. An event is caught and handled
by the handler class.
This mechanism was used by Java version 1.0, which
this required components to receive events that they did
not process & it wasted valuable time.
The delegation event modeleliminates
this overhead.(Java version 1.1 onwards)
7. The Delegation Event Model
The delegation event model defines standard and consistent mechanisms to generate
and process events.
Principle:
A source generates an event and sends it to one or more listeners.
The listener waits until it receives an event.
Once an event is received, the listener processes the event and then returns.
Advantage:
The application logic that processes events is cleanly separated from the user
interface logic that generates those events.
A user interface element is able to “delegate” the processing of an event to a separate
piece of code.
In the delegation event model, listeners must register with a source in order to
receive an event notification.
8. Event
Changing the state of an object is known as an event.
They are external effects that are controlled by the user
For example, click on button, dragging mouse etc.
The event object is used to carry the required information about the
state change.
When we click on the "click me" button an event is generated; that
change event generates another frame that shows our message, that
is passed to the program.
9. When you press a button in your program or Android
application the state of the button changes from ‘Unclicked’
to ‘Clicked’. This change in the state of our button is called
an Event.
10. Types of Event
1. Foreground Events
Those events which require the direct interaction of user.
They are generated as consequences of a person interacting with
the graphical components in Graphical User Interface.
For example, clicking on a button, moving the mouse, entering a
character through keyboard,selecting an item from list, scrolling
the page etc.
Foreground Events
Background Events
11. 2.Background Events
Those events that require the interaction of end user are
known as background events.
Operating system interrupts, hardware or software
failure, timer expires, an operation completion are the
example of background events.
12. Event Sources
A source is an object that generates an event
Event Sources are responsible for generating events and are
called components.
It also provides information about the event to the listener
Event sources could be anything from text boxes and combo
boxes to buttons, and more
Sources may generate more than one type of event
13.
14. Listeners
A listener is an object that listens to the event. A listener gets
notified when an event occurs
Events are handled by a special group of interfaces, known as
"listeners".
Listeners are also called as event handlers as they are the ones
responsible to handle events occurring at the source.
Listeners are interfaces and different types of listeners are used
according to the event
It has two major requirements. First, it must have been
registered with one or more sources to receive notifications
about specific types of events.
Second, it must implement methods to receive and process
these notifications.
15. Here is the general form
public void addTypeListener(TypeListener el)
For example:
Button b=new Button("click me");
b.addActionListener(this);
Here, type is the name of the event, and el is a reference to the
event listener.
For example, the method that registers a keyboard event listener is
called addKeyListener().
The method that registers a mouse motion listener is called
addMouseMotionListener().
When an event occurs, all registered listeners are notified and receive
a copy of the event object. This is known as multicasting the event.
In all cases, notifications are sent only to listeners that register to
receive them
16. A source must also provide a method that allows a listener to
unregister an interest in a specific type of event. The general form
of such a method is this:
Public void removeTypeListener(TypeListener el)
Here, type is an object that is notified when an event listener. For
example, to remove a keyboard listener, you would call
removeKeyListener()
17. Important Event Classes and Interface
EVENT CLASSES DESCRIPTION LISTENER INTERFACE
ActionEvent generated when button is
pressed, menu-item is selected,
list-item is double clicked
ActionListener
MouseEvent generated when mouse is
dragged,
moved,clicked,pressed or
released and also when it
enters or exit a component
MouseListener
KeyEvent generated when input is
received from keyboard
KeyListener
ItemEvent generated when check-box or
list item is clicked
ItemListener
TextEvent generated when value of
textarea or textfield is changed
TextListener
MouseWheelEvent generated when mouse wheel
is moved
MouseWheelListener
18. WindowEvent generated when window is
activated, deactivated,
deiconified, iconified,
opened or closed
WindowListener
ComponentEvent generated when
component is hidden,
moved, resized or set
visible
ComponentListener
ContainerEvent generated when
component is added or
removed from container
ContainerListener
AdjustmentEvent generated when scroll bar
is manipulated
AdjustmentListener
FocusEvent generated when
component gains or loses
keyboard focus
FocusListener
20. How Events are handled ?
A source generates an Event and send it to one or more listeners
registered with the source. Once event is received by the listener,
they process the event and then return.
Each type of event has its own registration method
21. Steps to handle events:
Implement appropriate interface in the class.
Register the component with the listener
Registration Methods
For registering the component with the Listener, many classes
provide the registration methods
22. For example:
Button
public void addActionListener(ActionListener a){}
MenuItem
public void addActionListener(ActionListener a){}
TextField
public void addActionListener(ActionListener a){}
public void addTextListener(TextListener a){}
TextArea
public void addTextListener(TextListener a){}
Checkbox
public void addItemListener(ItemListener a){}
Choice
public void addItemListener(ItemListener a){}
List
public void addActionListener(ActionListener a){}
public void addItemListener(ItemListener a){}