Action and Event Listener
Action and Event Listener
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
• 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.
1
4/7/2019
• 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
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
• 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
• Interface declaration
• Following is the declaration
for java.awt.event.ItemListener interface:
• 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
• 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