Read More - Myknowledge

Download Report

Transcript Read More - Myknowledge

Fundamental of Java Programming
(630002)
Unit – 5
Event Handling
The Event Delegation Model
The main difference between application and
applet is applications provides console applications
while Applets provides GUI. No doubt this is the
difference between applications and applets but
the other difference is GUI based programs are
event-driven while applications are not.
Our applets are also event-driven. That is the
code to handle a particular type of event is invoked
only when such an event occurs.
The Event Delegation Model
The java runtime system notifies the applet
about an event by calling an event handler that has
been supplied by the applet. Upon receipt of the
event, the applet handles the event and then
returns control to the run-time system.
There are various ways in which an eventdriven interface can be defined, and the specific
mechanism is determined by the event model. Java
Development Kit (JDK 1.1) introduce the delegation
event model.
The Event Delegation Model
This model provides an efficient way for an
event to be sent from a source to one or more
listeners. This model also provides a standard
mechanism for a source to generate an event and
send it to a set of listeners.
An Event
An event is an object that describes some
state change in a source. It can be generated when
a person interacts with an element in a GUI. For
example, pressing a button, clicking the mouse,
double clicking on a list box entry, or closing a
window.
A source generates events. It has mainly
three responsibilities.
An Event
First, it must provide methods that allow
listeners to register and unregister for notifications
about a specific type of event. Second, it must
generate the event. And finally, it must send the
event to all the registered listeners. Sometimes it
may happen that a source may generate several
types of events in that case multiple methods
would be provided.
An Event
We can add / remove the listener in the
following way :
(1) public void addTypeListner(TypeListner el)
(2) public void removeTypeListener(TypeListener el)
Here the type is the type of the event and el is
the event listener
The Listener
The listener receives event notifications. It
has three main responsibilities. First, it must
register to receive notifications about specific
events. It is possible by calling the appropriate
registration method of the source. Second it must
implement an interface to receive events of that
type. And finally, it must unregister if it no longer
wants to receive those notifications.
The Event Delegation Model can be described
through the following figure.
The Event Delegation Model
Listener
Source
Events
Listener
Listner
Various Event Classes
A set of classes are provided to represent the
various types of AWT events. Which can be
summarized through the following figure.
The EventObject class extends Object and is a
part of java.util package. Its constructor is :
EventObject (Object src)
Here, src is the object that generates the
events.
Various Event Classes
Object
EventObject
AWTEvent
ActionEvent
AdjustmentEvent
ContainerEvent
ComponentEvent
FocusEvent
InputEvent
KeyEvent
ItemEvent
TextEvent
WindowEvent
MouseEvent
Various Event Classes
Event
Generated When
ActionEvent
A Button is pressed, a list item is double-clicked, or a menu item is
selected
AdjustmentEvent A Scroll bar is manipulated
ComponentEvent A component is hidden, moved, resized or becomes visible
ContainerEvent
A component is added to or removed from a container
FocusEvent
A component gains or loses keyboard focus
InputEvent
A mouse or key event occurs
ItemEvent
A checkbox or list item is clicked, a choice selection is made, or a
checkable menu item is selected or deselected.
KeyEvent
Input is received from the keyboard
MouseEvent
The mouse is dragged or moved, clicked, pressed or released. Also
generated when the mouse enters or exists a component
TextEvent
The value of text area or text field is changed
WindowEvent
A window is activated, closed, deactivated, deiconified, iconified,
open, or quit.
Event Listeners
The java.util.EventListener interface does not
define any constants or methods but exists only to
identify those interfaces that process events. All
event listeners interface must extends this
interface. Various listeners for the various events
are summarized in the following table.
Remember that all the listeners are defined in
java.awt.event package.
Event Listeners
Event Class
Listener Interface
ActionEvent
ActionListener
AdjustmentEvent
AdjustmentListener
ComponentEvent
ComponentListener
ContainerEvent
ContainerListener
FocusEvent
FocusListener
ItemEvent
ItemListener
KeyEvent
KeyListener
MouseEvent
MouseListener, MouseMotionListener
TextEvent
TextListener
WindowEvent
WindowListener
Event Listeners
ex\events\MouseEventsDemo.java
ex\events\MouseMotionEvents.java
Exercise :
By using various mouse events draw happy
face applet.
Adapter Classes
In last example we have seen that the
MouseListener interface declares five methods.
These are called when the mouse is clicked, pressed
or released. Mouse events also occur when the
mouse enters or exits a component.
The main problem here is it has to implement
all the five methods declared by that interface even
though we don’t require them. This is very much
inconvenient.
Adapter Classes
Adapter classes make it easy to deal with this
situation.
An adapter class provides empty
implementation of all methods in a particular
listener interface. It can be useful if you want to
override only some of the methods defined by the
interface.
Various adapter classes for various listener
interfaces are as follows :
Adapter Classes
Adapter Class
Listener Class
ComponentAdapter
ComponentListener
ContainerAdapter
ContainerListener
FocusAdapter
FocusListener
KeyAdapter
KeyListener
MouseAdapter
MouseListener
MouseMotionAdapter
MouseMotionListener
WindowAdapter
WindowsListener
ex\events\AdapterClassDemo.java
Exercise : Use adapter class to write an applet that
changes background to cyan while the mouse being
dragged. All other times it should be white
Inner Classes
An inner class is defined within the scope of
an expression or another class. Therefore it has
access to the variables and methods in that scope.
We have already learned about the inner
classes in previous unit no. 2. Here we will see how
we can use inner classes with adapter for mouse
events.
Here we will define the adapter class inside
the main class.
Inner Classes
ex\events\InnerClassDemo.java
When we compile the above file It will
generate two .class files.
These are
MouseInnerDemo.class
and
MouseInnerDemo$MyMouseAdapter.class
Exercise :
Use inner class to write an applet that changes
background to cyan while the mouse being
dragged. All other times it should be white
Anonymous Inner Classes
An anonymous inner class is an inner class
that does not have a name.
Anonymous inner class will be declared
generally inside the method by using new keyword
with the class.
ex\events\AnonymousInnerClassDemo.java
Exercise :
Use Anonymous inner class to write an applet that
changes background to cyan while the mouse being
dragged. All other times it should be white
Exercise
General Exercise of Event Handling
(1) Write an applet which appears with its
background color equal to black. Each time the
mouse is clicked, the background color becomes
little brighter. In other words successive clicks of
mouse makes the background color from black
to white.
(2) Write an applet whose display area smoothly
changes color from one color to another. If we
press mouse it will stop changing. If we again
press the mouse the changes again starts.
Thank You