0% found this document useful (0 votes)
5 views

Lesson 5 Event Driven Programming in JAVA

Uploaded by

kristenlevee
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Lesson 5 Event Driven Programming in JAVA

Uploaded by

kristenlevee
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 18

COMPUTER

PROGRAMMIN
G II
TODAY’S LESSON
Event-Driven Programming and I/O Control
Function

LEARNING OUTCOMES
At the end of the lesson, students should be able to:
design programs that handle events to implement event
driven applications
EVENT-DRIVEN
PROGRAMMING
Changing the state of an object is known as an event. For
example, click on button, dragging mouse etc. The
java.awt.event package provides many event classes and
Listener interfaces for event handling.
Java Event
Event Classesclasses and Listener interfaces:
Listener Interfaces
ActionEvent ActionListener
MouseEvent MouseListener and MouseMotionListener
MouseWheelEvent MouseWheelListener
KeyEvent KeyListener
ItemEvent ItemListener
TextEvent TextListener
AdjustmentEvent AdjustmentListener
WindowEvent WindowListener
ComponentEvent ComponentListener
ContainerEvent ContainerListener
FocusEvent FocusListener
STEPS TO PERFORM EVENT
HANDLING:
Following steps are required to perform event handling:
1.Register the component with the Listener
Registration Methods:
For registering the component with the Listener, many
classes provide the registration methods. For example:
Components Registration Methods
JButton public void addActionListener(ActionListener a){}
JMenuItem public void addActionListener(ActionListener a){}
JTextField public void addActionListener(ActionListener a){}
public void addTextListener(TextListener a){}
JTextArea public void addTextListener(TextListener a){}
JCheckBox public void addItemListener(ItemListener a){}
JComboBox public void addItemListener(ItemListener a){}
JList public void addActionListener(ActionListener a){}
public void addItemListener(ItemListener a){}
How Events are handled?
A source generates an Event and send it to one or more listeners
registered with the source. Once event is received by the listener, they
process the event and then return. Events are supported by a number of Java
packages,
Event like java.util,
Classes java.awt and java.awt.event.
Description Listener Interface
ActionEvent generated when button is pressed, menu-item ActionListener
is selected, list-item is double clicked
MouseEvent generated when mouse is dragged, MouseListener
moved,clicked,pressed or released and also
when it enters or exit a component
KeyEvent generated when input is received from KeyListener
keyboard
ItemEvent generated when check-box or list item is ItemListener
clicked
TextEvent generated when value of textarea or textfield is TextListener
changed
MouseWheelEve generated when mouse wheel is moved MouseWheelListener
nt
WindowEvent generated when window is activated, WindowListener
deactivated, deiconified, iconified, opened or
closed
ComponentEven generated when component is hidden, moved, ComponentEventList
t resized or set visible ener
HANDLING ACTION EVENTS IN JAVA
The Java ActionListener is notified whenever you click on
the button or menu item. It is notified against ActionEvent. The
ActionListener interface is found in java.awt.event package. It
has only one method:

actionPerformed().
actionPerformed() method

The actionPerformed() method is invoked automatically


whenever you click on the registered component.
HOW TO WRITE ACTIONLISTENER:
The common approach is to implement the ActionListener. If you
implement the ActionListener class, you need to follow 3 steps:

1. Implement the ActionListener interface in the class:

public class ActionListenerExample Implements ActionListener

2. Register the component with the Listener:

component.addActionListener(instanceOfListenerclass);

3. Override the actionPerformed() method:


public void actionPerformed(ActionEvent e){
//Write the code here
}
HANDLING MOUSE CLICKS IN JAVA
The MouseListener
The Java MouseListener is notified whenever you change the state of
mouse. It is notified against MouseEvent. The MouseListener interface is
found in java.awt.event package. It has five methods.

Methods of MouseListener interface:


The signature of 5 methods found in MouseListener interface are given
below:
public abstract void mouseClicked(MouseEvent e);
public abstract void mouseEntered(MouseEvent e);
public abstract void mouseExited(MouseEvent e);
public abstract void mousePressed(MouseEvent e);
public abstract void mouseReleased(MouseEvent e);
THE MOUSEMOTIONLISTENER
The Java MouseMotionListener is notified whenever you
move or drag mouse. It is notified against MouseEvent. The
MouseMotionListener interface is found in java.awt.event
package. It has two methods.
public abstract void
mouseDragged(MouseEvent e);
public abstract void
mouseMoved(MouseEvent e);
HOW TO USE KEYLISTENER IN JAVA
The Java KeyListener is notified whenever you change the
state of key. It is notified against KeyEvent. The KeyListener
interface is found in java.awt.event package. It has three methods.

Methods of KeyListener interface

The signature of 3 methods found in KeyListener interface are


given below:
public abstract void keyPressed(KeyEvent e);
public abstract void keyReleased(KeyEvent e);
public abstract void keyTyped(KeyEvent e);
ITEM EVENTS IN JAVA

The Java ItemListener is notified whenever you click on the


checkbox. It is notified against ItemEvent. The ItemListener
interface is found in java.awt.event package. It has only one
method: itemStateChanged().

itemStateChanged() method

The itemStateChanged() method is invoked automatically


whenever you clickpublic
or unclick on the
abstract registered checkbox
void
component.
itemStateChanged(ItemEvent e);
THANK
YOU!

You might also like