Event Handling
Event Handling
SHYNA
What is an Event?
GUI components communicate with the rest of the applications
through events.
EVENT: an object that describes the state change in a source.
Eg: Clicking on a button,Moving the mouse,Selecting an item
from the list
EVENT HANDLING
A mechanism that controls the events and decides what should
happen if an event occurs.
Delegation Event Model
• model defines the standard mechanism to
generate and handle the events
• key participants are
• Event
• Event Source
• Event Listner
• Ev
09-05-2018 RSET
The source of an event is the component that causes that event to
occur.-EVENT SOURCE
• Source is responsible for providing information of the occurred
event to it's handler.
09-05-2018 RSET
To be notified for an event,
09-05-2018 RSET
Advantages:user interface logic is completely
separated from the logic that generates the event.
The user interface element is able to delegate the
processing of an event to the separate piece of
code.
09-05-2018 RSET
The Event Handling process
1. The user performs an action, causing an event to be triggered
(or fired).
2. An object is created that contains information about the
event, including an indication of which component was
involved
3. A method that belongs to a listener object is called. The object
created in step 2 is passed to the method.
09-05-2018 RSET
The Event classes
An event object has an event class as its reference data type.
The Event object class
Defined in the java.util package.
The AWT Event class
An immediate subclass of EventObject.
Defined in java.awt package.
Root of all AWT based events.
Hierarchy of event objects
Courtesy: Safari.oreilly.com
Events
High Level Events
Class Name Description of Event
ActionEvent A significant action has been performed on
a component (a button was pressed, a list
item was double-clicked, or the Enter key
was pressed in a text field).
AdjustmentEvent The state of an adjustable component (such
as a scrollbar) has changed.
ItemEvent An item has been selected (or deselected)
within a checkbox, choice menu, or list.
TextEvent The contents of a text area or text field
have changed.s
. 12
Event classes for GUI controls
09-05-2018 RSET
Event classes for GUI controls
09-05-2018 RSET
Types of Listener Interface
Example:
public void
actionPerformed(ActionEvent e)
The above code contains the handler for the ActionEvent e
that occurred.
The MouseListener interface
Event handling when the mouse is clicked.
public void mouseClicked(MouseEvent
e)
Event handling when the mouse enters a component.
public void mouseEntered(MouseEvent
e)
Event handling when the mouse exits a component.
public void mouseExited(MouseEvent
e)
The MouseListener Methods (contd..)
public void
actionPerformed(ActionEvent e) {
...//code that reacts to the
action...
}
An example of Event Handling
The other way to determine the origin of a button press is to use the
getActionCommand method.
String label = evt.getActionCommand();
if (label.equals("Testing")) …
s
09-05-2018 RSET
Adapter classes for Event Handling.