event listener

Download Report

Transcript event listener



Interface
Event Handling



Using interface, we specify what a class must do, but
not how it does this.
An interface is syntactically similar to a class, but it
lacks instance variables and its methods are declared
without any body.
An interface is defined with an interface keyword.

Two types of access:
◦ public – interface may be used anywhere in a program
◦ default – interface may be used in the current package only



Interface methods have no bodies – they end with the
semicolon after the parameter list. They are essentially
abstract methods.
An interface may include variables, but they must be
final, static and initialized with a constant value.
In a public interface, all members are implicitly public.

A class implements an interface if it provides a
complete set of methods defined by this interface.
◦ any number of classes may implement an interface
◦ one class may implement any number of interfaces


Each class is free to determine the details of its
implementation.
Implementation relation is written with the implements
keyword.

Declaration of the Callback interface:

Client class implements the Callback interface:

An event is an action initiated by the user interacting
with the program.

Examples
◦ Keyboard events - pressing a key, holding a key, releasing a key
◦ Mouse events - moving the mouse, clicking the mouse
◦ GUI events - clicking on a button, resizing a window, closing a window,
opening a window

An event in Java is an object of a particular event class, that
represents some user actions to which the GUI might respond


Low level events represent direct communication from the user
Low level event examples (all the event classes listed below
belong to the java.awt.event package:
◦ key event - a keyboard key pressed or released - in the KeyEvent class
◦ focus event – a component got focus, lost focus – in the FocusEvent class
◦ mouse event - the mouse is moved or dragged, a mouse button is pressed
or released, the mouse cursor enters or exits a component - in the
MouseEvent class
◦ component event - a component is hidden, shown, resized, or moved – in
the ComponentEvent class
◦ container event - a component is added to or removed from a container in
the ContainerEvent class
◦ window event - a window is opened, closed, activated, deactivated, etc. in the WindowEvent class


High level events usually involve one or more low level
events
High Level Event examples
◦ action event - do a command – ActionEvent class
◦ adjustment event - represents scrollbar motions such as a value
was adjusted – AdjustmentEvent class
◦ item event - occurs when the user selects a checkbox, choice, or
list item, i.e. item state has changed –ItemEvent class
◦ text event – represents a text component content (value) change –
TextEvent class

When the user clicks the mouse on a button, then
releases it, the button gets two or three separate, low
level mouse events
◦ one for mouse down
◦ one for mouse up
◦ possibly one for mouse drag (if the user moves the mouse
while the button is pressed)

However, the button then fires one high level event
only - ActionEvent



Events are organized into hierarchy of event classes
Event classes contain data relevant to a particular
event type
An event is an object of one of the event classes
java.lang.Object
java.util.EventObject
java.awt.AWTEvent
ActionEvent
ContainerEvent
AdjustmentEvent
FocusEvent
KeyEvent
ComponentEvent
InputEvent
ItemEvent
PaintEvent
MouseEvent
TextEvent
WindowEvent


The type of an event depends on its source
Example of event sources:
◦ the keyboard
◦ the mouse
◦ the GUI components – buttons, text fields, windows

Event source is an object with the ability to
determine when an event has occurred




In event driven programming the events “drive” the
execution of the program, e.g. the code is executed when
events are activated
The program interacts with the user and generates events
based on the external user actions
Java Visual (Graphical) programming and Visual Basic
programming are event driven
When writing applets that are using events in Java we have
to import the “events” package java.awt.event.*;



Java uses delegation-based model for event
handling
Java uses event listener to register an event and
event handler to respond to the event
The use of event listeners in event handling is
called delegation event model


An external user’s action on a source object (e.g. the event
source) activates an event
An event listener object (e.g. an object interested in the
event source) receives the event. This object is an instance
of a class that implements a specific EventListener
interface
◦ Example:

ActionEvent --> ActionListener
The source maintains a list containing all the listener objects
that have registered to be notified of events of that type

The transmission of an event from an event source to an
event listener involves invoking a method on the listener
object by which the source notifies the listener of the
occurrence of an event of a specific type
◦ Example: method actionPerformed (ActionEvent e)
◦ An EventListener interface declares one or more methods
which must be defined in the listener class, and which are
invoked by the event source in response to each specific event
type handled by the interface
 Example:
EventListener  method actionPerformed (ActionEvent e)




The Java standard class library contains several classes that
represent typical events
Components, such as an applet or a button, generate (fire) an
event when it occurs
Objects, called listeners, wait for events to occur. A listener
object is an instance of a class that implements a specific
listener interface
A number of listener interfaces are pre-defined and each
interface declares the appropriate methods for a specific class
of events
Event
Component
Listener
This object may
generate an event
This object waits for and
responds to an event
When an event occurs, the component calls
the appropriate method of the listener,
passing an (event) object that describes the event


Each event is
represented by an
object that gives
information about the
event and identifies the
event source.
Each event source can
have multiple listeners
registered on it. A
single listener can
register with multiple
event sources.
event listener 1
event listener 2
event source
event listener 3



A listener object can be registered on a source object to be
notified of the occurrence of all events of the specific class
for which the listener object is designed
The occurrence of an event defined by the specified class
will automatically invoke the matching method in the
listener object
The code in the body of the method is designed by the
programmer to perform the desired action when the event
occurs



We can create a listener object by writing a class
that implements a particular listener interface
The Java standard class library contains several
interfaces that correspond to particular event
categories
After creating the listener, we add the listener to the
component that might generate the event to set up a
relationship between the component, generating the
event and the event listener

Register an event listener
◦ “listens” for events generated by GUI components
◦ an object of a class from the package java.awt.event

Implement an event handler
◦ a method that is automatically called in response to a particular
type of event


For each event class there is a corresponding listener
interface defined in Java and corresponding listener
methods (handlers) in the listener interface
Example :
◦ for the event class ActionEvent
◦ the listener is ActionListener
◦ and the listener method (handler) is actionPerformed
(ActionEvent e )

Commonly used AWT event Listeners
◦
◦
◦
◦
◦
◦
◦
◦
◦
◦
◦
ActionListener
AdjustmentListener
FocusListener
ItemListener
KeyListener
MouseListener
MouseMotionListener
MouseWheelListener
WindowListener
WindowFocusListener
WindowStateListener

Every event handler requires three separate steps
1.
In the declaration for the event handler class, we specify that the
class either implements a listener interface or extends a class that
implements a listener interface
public class MyClass implements ActionListener { …
2.
}
Code that registers an instance of the event handler class as a listener
upon one or more components
someComponent.addActionListener(instanceOfMyClass);
3.
Code that implements the methods in the listener interface
public void actionPerformed(ActionEvent e) {
//code that reacts to the action... }




Adapter Class exists as convenience for creating a listener object.
Extend this class to create a listener for a particular listener
interface and override the methods for the events of interest.
It defines null methods for all of the methods in the listener
interface, so you can only have to define methods for events you
care about.
Commonly used adapter classes:
◦
◦
◦
◦
◦
FocusAdapter
KeyAdapter
MouseAdapter
MouseMotionAdapter
WindowAdapter