Lesson 5 Event Driven Programming in JAVA
Lesson 5 Event Driven Programming in JAVA
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
component.addActionListener(instanceOfListenerclass);
itemStateChanged() method