2. Introduction
• In this chapter, you will learn about Events, its types, and also
learn how to handle an event.
• Example is provided in this chapter for better understanding.
2
3. What is an Event?
• Change in the state of an object is known as Event,
• i.e., event describes the change in the state of the source.
• Events are generated as a result of user interaction with the
graphical user interface components.
• For example,
clicking on a button,
moving the mouse,
entering a character through keyboard
selecting an item from the list, and
scrolling the page are the activities that causes an event to occur.
3
4. Types of Event
• The events can be broadly classified into two categories
A. Foreground Events and B. Background Events
4
5. Types of Event
• The events can be broadly classified into two categories
A. Foreground Events
• These events require direct interaction of the user.
• They are generated as consequences of a person interacting
with the graphical components in the Graphical User
Interface.
• For example, clicking on a button, moving the mouse,
entering a character through keyboard, selecting an item
from list, scrolling the page, etc.
5
6. ..cont
B. Background Events
• These events not require the interaction of the end user.
• Operating system interrupts,
• hardware or software failure,
• timer expiration, and
• operation completion are some examples of background
events.
6
7. What is Event Handling?
• Event Handling is the mechanism that controls the
event and decides what should happen if an event
occurs.
• This mechanism has a code which is known as an
Event Handler, that is executed when an event occurs.
• Java uses the Delegation Event Model to handle the
events. This model defines the standard mechanism to
generate and handle the events.
7
8. ..cont
The Delegation Event Model has the following key participants
A. Source
• The source is an object on which the event occurs.
• Source is responsible for providing information of the occurred
event to it's handler.
B. Listener
• It is also known as event handler.
• The listener is responsible for generating a response to an event.
• The listener waits till it receives an event.
• Once the event is received, the listener processes the event and
then returns.
8
9. Handling Event In Java
• The user interfaces we’ve created so far are not
interactive—nothing happens when the user clicks the
buttons or types on the components.
• In order to create useful interactive GUIs, you must learn
how to handle Java events.
• When the user clicks on a component, moves the mouse
over it, or otherwise interacts with it, Java’s GUI system
creates a special kind of object called an event to
represent this action.
9
10. Cont…
• By default, if you don’t specify how to react to an event, it
goes unnoticed by your program.
• Therefore, nothing happens when the user clicks your buttons
or types in your text fields.
• You can cause the program to respond to a particular event
(such as the user clicking a button) by using an object called a
listener.
• Listener is an object that is notified when an event occurs and
that executes code to respond to the event.
• To handle an event, create a listener object and attach it to
the component of interest.
• The listener object contains the code that you want to run
when the appropriate event occurs. 10
11. Cont…
• The first kind of event we’ll handle in this chapter is called
an action event.
• An action event is a fairly general type of event that
occurs when the user interacts with many standard
components (for example, clicking on a button or
entering text into a JTextField).
• In Java, event listeners are written by implementing
particular interfaces.
• The interface for handling action events in Java is called
ActionListener.
11
12. Cont…
• The ActionListener interface contains only the following
method:
public void actionPerformed(ActionEvent event)
• To listen to an event, you write a class that implements
the ActionListener interface and place into its
actionPerformed method the code that you want to run
when the event occurs.
• Then you attach an object of your listener class to the
appropriate component.
12
13. Event Handling Example 1
13
import java.awt.*;
import javax.swing.*;
public class EventHandlingLab1 {
private static JLabel l1,l2;
public static void main(String[] args) {
JButton b1 = new JButton("OK");
JButton b2 = new JButton("Cancel");
l1 = new JLabel("Event : ");
l2 = new JLabel("");
JPanel p1 = new JPanel();
JPanel p2 = new JPanel();
p1.add(b1);
p1.add(b2);
p2.add(l1);
p2.add(l2);
JFrame f = new JFrame();
f.setLayout(new GridLayout(3, 1));
f.setTitle("JFrame Example");
f.setLocation(300, 100);
f.setSize (400,300);
f.add(p1);
f.add(p2);
f.setVisible(true);
14. import javax.swing.*;
public class JOptionExample {
public static void main(String[] args) {
String num1 = JOptionPane.showInputDialog("Enter first integer");
String num2 = JOptionPane.showInputDialog("Enter second integer");
int number1 = Integer.parseInt(num1);
int number2 = Integer.parseInt(num2);
int sum = number1 + number2; // add numbers
// display result in a JOptionPane message dialog
JOptionPane.showMessageDialog(null, "The sum is " + sum, "Sum of Two
Integers", JOptionPane.PLAIN_MESSAGE);
}
}
14
Event Handling Example 1
15. Event Handling Example 2
15
import java.awt.*;
import javax.swing.*;
public class EventHandlingLab1 {
private static JLabel l1,l2;
public static void main(String[] args) {
JButton b1 = new JButton("OK");
JButton b2 = new JButton("Cancel");
l1 = new JLabel("Event : ");
l2 = new JLabel("");
JPanel p1 = new JPanel();
JPanel p2 = new JPanel();
p1.add(b1);
p1.add(b2);
p2.add(l1);
p2.add(l2);
JFrame f = new JFrame();
f.setLayout(new GridLayout(3, 1));
f.setTitle("JFrame Example");
f.setLocation(300, 100);
f.setSize (400,300);
f.add(p1);
f.add(p2);
f.setVisible(true);