6
Most read
11
Most read
Chapter 2
Event Handling
1
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
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
Types of Event
• The events can be broadly classified into two categories
A. Foreground Events and B. Background Events
4
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
..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
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
..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
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
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
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
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
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);
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
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);
b1.addActionListener(new ButtonListener());
b2.addActionListener(new ButtonListener());
}
private static class ButtonListener implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
String command = e.getActionCommand();
if( command.equals( "OK" )) {
l2.setText("Ok Button clicked.");
else {
l2.setText("Cancel Button clicked.");
}
}
16
Cont….
THANK YOU
17

More Related Content

PPTX
ITE 1122_ Event Handling.pptx
PDF
Java Programming :Event Handling(Types of Events)
PPTX
EVENT HANDLING in Object Oriented Programming in Java
PPTX
Advance Java Programming(CM5I) Event handling
PPTX
Event Handling in Java
PPTX
event-handling.pptx
PPTX
Event handling
PPT
Chapter 8 event Handling.ppt m m m m m m
ITE 1122_ Event Handling.pptx
Java Programming :Event Handling(Types of Events)
EVENT HANDLING in Object Oriented Programming in Java
Advance Java Programming(CM5I) Event handling
Event Handling in Java
event-handling.pptx
Event handling
Chapter 8 event Handling.ppt m m m m m m

Similar to Chap - 2 - Event Handling.pptx (20)

PPTX
Event handling in Java(part 1)
PPTX
Event Handling in JAVA
PPT
Events1
PDF
JAVA PROGRAMMING- GUI Programming with Swing - The Swing Buttons
PPT
Graphical User Interface (GUI) - 2
PPTX
Advance java programming- Event handling
PPT
event_handling.ppt
PPT
Java gui event
PDF
JAVA GUI PART III
PDF
java-programming GUI- Event Handling.pdf
PDF
Event handling
PDF
Event handling
PPTX
What is Event
PPT
event handling new.ppt
PPS
Java session11
PDF
Event Handling in Java as per university
PPTX
PROGRAMMING IN JAVA- unit 4-part II
PPTX
Chapter 11.5
PDF
Ajp notes-chapter-03
Event handling in Java(part 1)
Event Handling in JAVA
Events1
JAVA PROGRAMMING- GUI Programming with Swing - The Swing Buttons
Graphical User Interface (GUI) - 2
Advance java programming- Event handling
event_handling.ppt
Java gui event
JAVA GUI PART III
java-programming GUI- Event Handling.pdf
Event handling
Event handling
What is Event
event handling new.ppt
Java session11
Event Handling in Java as per university
PROGRAMMING IN JAVA- unit 4-part II
Chapter 11.5
Ajp notes-chapter-03
Ad

More from TadeseBeyene (20)

PPTX
oprate database Application program hardware
PPTX
chp unit 1 Provide Network System Administration.pptx
PPTX
micro chapter 3jjgffffyeyhhuyerfftfgggffgjj
PPTX
Chapter fourvvvvvvvbbhhgggghhhhhhheryuuuhh
PPTX
Topology Chapter 2.pptx
PPTX
Chapter four.pptx
PPTX
install and manage network protocol .pptx
PPTX
Moniter & Administrator Network & System Security.pptx
PPTX
monitor and Admin Network .pptx
PPTX
provide1923.pptx
PPTX
window configuration & Administration.pptx
PPTX
Assisit with devlopment.pptx
PPTX
Chapter 3 And 4.pptx
PPTX
chapter2.pptx
PPTX
pdf to ppt window configuration .pptx
PPTX
Chap 1 - Introduction GUI.pptx
PPTX
CHP 1 Apply.pptx
PPTX
Chap 1 - mobile Introduction.pptx
PPTX
chapter 1.pptx
PDF
installandmanagenetworkprotocols-221219130255-40dcfff3 (1).pdf
oprate database Application program hardware
chp unit 1 Provide Network System Administration.pptx
micro chapter 3jjgffffyeyhhuyerfftfgggffgjj
Chapter fourvvvvvvvbbhhgggghhhhhhheryuuuhh
Topology Chapter 2.pptx
Chapter four.pptx
install and manage network protocol .pptx
Moniter & Administrator Network & System Security.pptx
monitor and Admin Network .pptx
provide1923.pptx
window configuration & Administration.pptx
Assisit with devlopment.pptx
Chapter 3 And 4.pptx
chapter2.pptx
pdf to ppt window configuration .pptx
Chap 1 - Introduction GUI.pptx
CHP 1 Apply.pptx
Chap 1 - mobile Introduction.pptx
chapter 1.pptx
installandmanagenetworkprotocols-221219130255-40dcfff3 (1).pdf
Ad

Recently uploaded (20)

PDF
CB Công Nghiệp Slide .dh bách khoa đà nẵng
DOCX
web lab manual for fifth semester BE course fifth semester vtu belgaum
PDF
B461227.pdf American Journal of Multidisciplinary Research and Review
PDF
AI agent, robotics based Smart Construction 2025
PDF
BBC NW_Tech Facilities_30 Odd Yrs Ago [J].pdf
PPT
Module_1_Lecture_1_Introduction_To_Automation_In_Production_Systems2023.ppt
PDF
Traditional Programming vs Machine learning and Models in Machine Learning
PDF
PhD defense presentation in field of Computer Science
PPTX
Cloud Security and Privacy-Module-2a.pptx
PPTX
Ingredients of concrete technology .pptx
PPTX
MODULE 02 - CLOUD COMPUTING-Virtual Machines and Virtualization of Clusters a...
PPTX
1. Effective HSEW Induction Training - EMCO 2024, O&M.pptx
PPTX
5-2d2b20afbe-basic-concepts-of-mechanics.ppt
PDF
Application of smart robotics in the supply chain
PDF
CBCN cam bien cong nghiep bach khoa da năng
PDF
Recent Trends in Network Security - 2025
PPTX
MODULE 3 SUSTAINABLE DEVELOPMENT GOALSPPT.pptx
PPTX
sinteringn kjfnvkjdfvkdfnoeneornvoirjoinsonosjf).pptx
PDF
The Journal of Finance - July 1993 - JENSEN - The Modern Industrial Revolutio...
PPT
linux chapter 1 learning operating system
CB Công Nghiệp Slide .dh bách khoa đà nẵng
web lab manual for fifth semester BE course fifth semester vtu belgaum
B461227.pdf American Journal of Multidisciplinary Research and Review
AI agent, robotics based Smart Construction 2025
BBC NW_Tech Facilities_30 Odd Yrs Ago [J].pdf
Module_1_Lecture_1_Introduction_To_Automation_In_Production_Systems2023.ppt
Traditional Programming vs Machine learning and Models in Machine Learning
PhD defense presentation in field of Computer Science
Cloud Security and Privacy-Module-2a.pptx
Ingredients of concrete technology .pptx
MODULE 02 - CLOUD COMPUTING-Virtual Machines and Virtualization of Clusters a...
1. Effective HSEW Induction Training - EMCO 2024, O&M.pptx
5-2d2b20afbe-basic-concepts-of-mechanics.ppt
Application of smart robotics in the supply chain
CBCN cam bien cong nghiep bach khoa da năng
Recent Trends in Network Security - 2025
MODULE 3 SUSTAINABLE DEVELOPMENT GOALSPPT.pptx
sinteringn kjfnvkjdfvkdfnoeneornvoirjoinsonosjf).pptx
The Journal of Finance - July 1993 - JENSEN - The Modern Industrial Revolutio...
linux chapter 1 learning operating system

Chap - 2 - Event Handling.pptx

  • 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);
  • 16. b1.addActionListener(new ButtonListener()); b2.addActionListener(new ButtonListener()); } private static class ButtonListener implements ActionListener{ @Override public void actionPerformed(ActionEvent e) { String command = e.getActionCommand(); if( command.equals( "OK" )) { l2.setText("Ok Button clicked."); else { l2.setText("Cancel Button clicked."); } } 16