4/7/2019
CLO
• 1. apply knowledge and skills to create a user
interfaces for an interactive java
programming. (C3, P3, PLO1, PLO2)
• 2. design a program that integrates the
database and web to build a java web based
application. (C5, P3, PLO1, PLO2)
• 3. produce an interactive application program
3.0 Event Handling using appropriate Java programming
environment. (P4, A3, PLO2, PLO4)
3.1 Event Handling Concept
What is Event? What is Event Handling?
• GUI components communicate with the rest • Event Handling is the mechanism that
of the applications through events. controls the event and decides what should
• The source of an event is the component that happen if an event occurs.
causes that event to occur. • This mechanism have the code which is
• The listener of an event is an object that known as event handler that is executed
receives the event and processes it when an event occurs.
appropriately.
What is Event Handling? What is Event Handling?
• When the user acts on a Component
clicking it or pressing the Return key, for
example an Event object is created.
1
4/7/2019
What is Event Handling? Handling Events
• Java Uses the Delegation Event Model to handle the events.
• Source • Every time the user types a character or clicks the
– The source is an object on which event occurs. Source is mouse, an event occurs.
responsible for providing information of the occurred • Any object can be notified of any particular event.
event to it's handler. Java provide as with classes for source • To be notified for an event,
object.
The object has to be registered as an event
• Listener listener on the appropriate event source.
– It is also known as event handler. Listener is responsible for
generating response to an event. From java The object has to implement the appropriate
implementation point of view the listener is also an object. interface.
Listener waits until it receives an event. Once the event is
received , the listener process the event an then returns.
The Event Handling process What does an Event Handler require?
• It just looks for 3 pieces of code!
• When an event is triggered, the JAVA runtime first
determines its source and type. • First, in the declaration of the event handler class,
• If a listener for this type of event is registered with one line of code must specify that the class
the source, an event object is created. implements either a listener interface or extends a
• For each listener to this type of an event, the JAVA class that implements a listener interface.
runtime invokes the appropriate event handling public class DemoClass implements
method to the listener and passes the event object ActionListener {
as the parameter.
• Second, it looks for a line of code which • Third, the event handler must have a piece of code
registers an instance of the event handler class
that implements the methods in the listener
as a listener of one or more components
because, as mentioned earlier, the object must interface.
be registered as an event listener.
public void actionPerformed(ActionEvent e)
{
anyComponent.addActionListener(instanc ...//code that reacts to the action...
eOf DemoClass); }
2
4/7/2019
Types of Events Java Event classes and Listener interfaces
Event Classes Listener Interfaces
Act causing Event Listener Type
ActionEvent ActionListener
User clicks a button, presses Enter, typing in ActionListener
text field
MouseEvent MouseListener and MouseMotionListener
User closes a frame WindowListener MouseWheelEvent MouseWheelListener
Clicking a mouse button, while the cursor is MouseListener KeyEvent KeyListener
over a component ItemEvent ItemListener
User moving the mouse over a component MouseMotionListener TextEvent TextListener
AdjustmentEvent AdjustmentListener
Component becomes visible ComponentListener WindowEvent WindowListener
Table or list selection changes ListSelectionListener
ComponentEvent ComponentListener
ContainerEvent ContainerListener
FocusEvent FocusListener
Event Listeners
• onclick: Use this to invoke JavaScript upon clicking (a link, or form boxes)
• onload: Use this to invoke JavaScript after the page or an image has • Event listeners are the classes that implement the
finished loading.
<type> Listener interfaces.
• onmouseover: Use this to invoke JavaScript if the mouse passes by some
link Example:
• onmouseout: Use this to invoke JavaScript if the mouse goes pass some 1. ActionListener receives action events
link
2. MouseListener receives mouse events.
• onunload: Use this to invoke JavaScript right after someone leaves this
page
The ActionListener Method Action Listener Example
• It contains exactly one method. • The interface used to handle the events cause by
sources like Buttons, Menu Items, Enter Keys and
Double Click Mouse.
Example:
• The Following method need to override.
public void actionPerformed(ActionEvent e)
public void
The above code contains the handler for the actionPerformed(ActionEvent e)
ActionEvent e that occurred.
3
4/7/2019
Action Listener Example ITEM LISTENER
• Interface declaration
• Following is the declaration
for java.awt.event.ItemListener interface:
public interface ItemListener
extends EventListener
ITEM LISTENER ITEM LISTENER
• Interface methods • Methods inherited
void itemStateChanged(ItemEvent e) This interface inherits methods from the following
Invoked when an item has been selected or deselected interfaces:
by the user.
java.awt.EventListener
ITEM LISTENER Item Listener Example
• Methods inherited • Used for Radio Button, List, Choice and Checkbox
AWT controls.
This interface inherits methods from the following • The Following method need to override.
interfaces:
public void
itemStateChanged(ItemEvent e)
java.awt.EventListener
4
4/7/2019
Item Listener Example MouseMotionListener Example
• The interface used to handle event generated from
Mouse Source
• Methods are as follow:
public void mouseMoved(MouseEvent e)
public void mouseDragged(MouseEvent
e)
The MouseListener Methods The MouseListener Methods
• 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 MouseMotionListener Methods
• Event handling when the mouse button is • Invoked when the mouse button is pressed over a
pressed on a component. component and dragged. Called several times as the
public void mousePressed(MouseEvent e) mouse is dragged
• Event handling when the mouse button is public void mouseDragged(MouseEvent e)
released on a component. • Invoked when the mouse cursor has been moved
public void mouseReleased(MouseEvent e) onto a component but no buttons have been
pushed.
public void mouseMoved(MouseEvent e)
5
4/7/2019
The WindowListener Methods
• Invoked when the window is set to be the active
• Invoked when the window object is opened. window.
public void windowOpened(WindowEvent e) public void windowActivated(WindowEvent e)
• Invoked when the user attempts to close the • Invoked when the window object is no longer the
window object from the object’s system menu. active window
public void windowClosing(WindowEvent e) public void windowDeactivated(WindowEvent e)
• Invoked when the window object is closed as a • Invoked when the window is minimized.
public void windowIconified(WindowEvent e)
result of calling dispose (release of resources used • Invoked when the window is changed from the
by the source). minimized state to the normal state.
public void windowClosed(WindowEvent e) public void windowDeconified(WindowEvent e)
Adapter classes for Event Handling.
• Why do you need adapter classes?
Implementing all the methods of an interface
involves a lot of work.
If you are interested in only using some methods of
the interface.
• Adapter classes
Built-in in JAVA
Implement all the methods of each listener interface
with more than one method.
Implementation of all empty methods