SlideShare a Scribd company logo
1 of 17
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

Similar to Chap - 2 - Event Handling.pptx

Event+driven+programming key+features
Event+driven+programming key+featuresEvent+driven+programming key+features
Event+driven+programming key+features
Faisal Aziz
 
Chapter11 graphical components
Chapter11 graphical componentsChapter11 graphical components
Chapter11 graphical components
Arifa Fatima
 

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

Event handling
Event handlingEvent handling
Event handling
 
Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)
 
Unit 6 Java
Unit 6 JavaUnit 6 Java
Unit 6 Java
 
File Handling
File HandlingFile Handling
File Handling
 
Java gui event
Java gui eventJava gui event
Java gui event
 
Event+driven+programming key+features
Event+driven+programming key+featuresEvent+driven+programming key+features
Event+driven+programming key+features
 
What is Event
What is EventWhat is Event
What is Event
 
Events1
Events1Events1
Events1
 
Event handling
Event handlingEvent handling
Event handling
 
Event handling
Event handlingEvent handling
Event handling
 
event handling new.ppt
event handling new.pptevent handling new.ppt
event handling new.ppt
 
Module3.11.pptx
Module3.11.pptxModule3.11.pptx
Module3.11.pptx
 
09events
09events09events
09events
 
Ajp notes-chapter-03
Ajp notes-chapter-03Ajp notes-chapter-03
Ajp notes-chapter-03
 
Java eventhandling
Java eventhandlingJava eventhandling
Java eventhandling
 
Chapter11 graphical components
Chapter11 graphical componentsChapter11 graphical components
Chapter11 graphical components
 
Module 5.pptx
Module 5.pptxModule 5.pptx
Module 5.pptx
 
Synapseindia dotnet development chapter 14 event-driven programming
Synapseindia dotnet development  chapter 14 event-driven programmingSynapseindia dotnet development  chapter 14 event-driven programming
Synapseindia dotnet development chapter 14 event-driven programming
 
Javascript Browser Events.pdf
Javascript Browser Events.pdfJavascript Browser Events.pdf
Javascript Browser Events.pdf
 
B2. activity and intent
B2. activity and intentB2. activity and intent
B2. activity and intent
 

More from TadeseBeyene

chp unit 1 Provide Network System Administration.pptx
chp unit 1 Provide Network System Administration.pptxchp unit 1 Provide Network System Administration.pptx
chp unit 1 Provide Network System Administration.pptx
TadeseBeyene
 
micro chapter 3jjgffffyeyhhuyerfftfgggffgjj
micro chapter 3jjgffffyeyhhuyerfftfgggffgjjmicro chapter 3jjgffffyeyhhuyerfftfgggffgjj
micro chapter 3jjgffffyeyhhuyerfftfgggffgjj
TadeseBeyene
 
Chapter fourvvvvvvvbbhhgggghhhhhhheryuuuhh
Chapter fourvvvvvvvbbhhgggghhhhhhheryuuuhhChapter fourvvvvvvvbbhhgggghhhhhhheryuuuhh
Chapter fourvvvvvvvbbhhgggghhhhhhheryuuuhh
TadeseBeyene
 
install and manage network protocol .pptx
install and manage network protocol .pptxinstall and manage network protocol .pptx
install and manage network protocol .pptx
TadeseBeyene
 
Moniter & Administrator Network & System Security.pptx
Moniter & Administrator Network & System Security.pptxMoniter & Administrator Network & System Security.pptx
Moniter & Administrator Network & System Security.pptx
TadeseBeyene
 
monitor and Admin Network .pptx
monitor and Admin Network .pptxmonitor and Admin Network .pptx
monitor and Admin Network .pptx
TadeseBeyene
 
window configuration & Administration.pptx
window   configuration  & Administration.pptxwindow   configuration  & Administration.pptx
window configuration & Administration.pptx
TadeseBeyene
 
Assisit with devlopment.pptx
Assisit with devlopment.pptxAssisit with devlopment.pptx
Assisit with devlopment.pptx
TadeseBeyene
 
pdf to ppt window configuration .pptx
pdf to ppt window configuration .pptxpdf to ppt window configuration .pptx
pdf to ppt window configuration .pptx
TadeseBeyene
 
Chap 1 - Introduction GUI.pptx
Chap 1 - Introduction GUI.pptxChap 1 - Introduction GUI.pptx
Chap 1 - Introduction GUI.pptx
TadeseBeyene
 
installandmanagenetworkprotocols-221219130255-40dcfff3 (1).pdf
installandmanagenetworkprotocols-221219130255-40dcfff3 (1).pdfinstallandmanagenetworkprotocols-221219130255-40dcfff3 (1).pdf
installandmanagenetworkprotocols-221219130255-40dcfff3 (1).pdf
TadeseBeyene
 

More from TadeseBeyene (20)

chp unit 1 Provide Network System Administration.pptx
chp unit 1 Provide Network System Administration.pptxchp unit 1 Provide Network System Administration.pptx
chp unit 1 Provide Network System Administration.pptx
 
micro chapter 3jjgffffyeyhhuyerfftfgggffgjj
micro chapter 3jjgffffyeyhhuyerfftfgggffgjjmicro chapter 3jjgffffyeyhhuyerfftfgggffgjj
micro chapter 3jjgffffyeyhhuyerfftfgggffgjj
 
Chapter fourvvvvvvvbbhhgggghhhhhhheryuuuhh
Chapter fourvvvvvvvbbhhgggghhhhhhheryuuuhhChapter fourvvvvvvvbbhhgggghhhhhhheryuuuhh
Chapter fourvvvvvvvbbhhgggghhhhhhheryuuuhh
 
Topology Chapter 2.pptx
Topology Chapter 2.pptxTopology Chapter 2.pptx
Topology Chapter 2.pptx
 
Chapter four.pptx
Chapter four.pptxChapter four.pptx
Chapter four.pptx
 
install and manage network protocol .pptx
install and manage network protocol .pptxinstall and manage network protocol .pptx
install and manage network protocol .pptx
 
Moniter & Administrator Network & System Security.pptx
Moniter & Administrator Network & System Security.pptxMoniter & Administrator Network & System Security.pptx
Moniter & Administrator Network & System Security.pptx
 
monitor and Admin Network .pptx
monitor and Admin Network .pptxmonitor and Admin Network .pptx
monitor and Admin Network .pptx
 
provide1923.pptx
provide1923.pptxprovide1923.pptx
provide1923.pptx
 
window configuration & Administration.pptx
window   configuration  & Administration.pptxwindow   configuration  & Administration.pptx
window configuration & Administration.pptx
 
Assisit with devlopment.pptx
Assisit with devlopment.pptxAssisit with devlopment.pptx
Assisit with devlopment.pptx
 
Chapter 3 And 4.pptx
Chapter 3 And 4.pptxChapter 3 And 4.pptx
Chapter 3 And 4.pptx
 
chapter2.pptx
chapter2.pptxchapter2.pptx
chapter2.pptx
 
pdf to ppt window configuration .pptx
pdf to ppt window configuration .pptxpdf to ppt window configuration .pptx
pdf to ppt window configuration .pptx
 
Chap 1 - Introduction GUI.pptx
Chap 1 - Introduction GUI.pptxChap 1 - Introduction GUI.pptx
Chap 1 - Introduction GUI.pptx
 
CHP 1 Apply.pptx
CHP 1 Apply.pptxCHP 1 Apply.pptx
CHP 1 Apply.pptx
 
Chap 1 - mobile Introduction.pptx
Chap 1 - mobile Introduction.pptxChap 1 - mobile Introduction.pptx
Chap 1 - mobile Introduction.pptx
 
chapter 1.pptx
chapter 1.pptxchapter 1.pptx
chapter 1.pptx
 
installandmanagenetworkprotocols-221219130255-40dcfff3 (1).pdf
installandmanagenetworkprotocols-221219130255-40dcfff3 (1).pdfinstallandmanagenetworkprotocols-221219130255-40dcfff3 (1).pdf
installandmanagenetworkprotocols-221219130255-40dcfff3 (1).pdf
 
Resove network problem.pptx
Resove network problem.pptxResove network problem.pptx
Resove network problem.pptx
 

Recently uploaded

Maher Othman Interior Design Portfolio..
Maher Othman Interior Design Portfolio..Maher Othman Interior Design Portfolio..
Maher Othman Interior Design Portfolio..
MaherOthman7
 
Activity Planning: Objectives, Project Schedule, Network Planning Model. Time...
Activity Planning: Objectives, Project Schedule, Network Planning Model. Time...Activity Planning: Objectives, Project Schedule, Network Planning Model. Time...
Activity Planning: Objectives, Project Schedule, Network Planning Model. Time...
Lovely Professional University
 

Recently uploaded (20)

Research Methodolgy & Intellectual Property Rights Series 1
Research Methodolgy & Intellectual Property Rights Series 1Research Methodolgy & Intellectual Property Rights Series 1
Research Methodolgy & Intellectual Property Rights Series 1
 
Theory for How to calculation capacitor bank
Theory for How to calculation capacitor bankTheory for How to calculation capacitor bank
Theory for How to calculation capacitor bank
 
Maher Othman Interior Design Portfolio..
Maher Othman Interior Design Portfolio..Maher Othman Interior Design Portfolio..
Maher Othman Interior Design Portfolio..
 
Linux Systems Programming: Semaphores, Shared Memory, and Message Queues
Linux Systems Programming: Semaphores, Shared Memory, and Message QueuesLinux Systems Programming: Semaphores, Shared Memory, and Message Queues
Linux Systems Programming: Semaphores, Shared Memory, and Message Queues
 
Quiz application system project report..pdf
Quiz application system project report..pdfQuiz application system project report..pdf
Quiz application system project report..pdf
 
Artificial Intelligence Bayesian Reasoning
Artificial Intelligence Bayesian ReasoningArtificial Intelligence Bayesian Reasoning
Artificial Intelligence Bayesian Reasoning
 
Supermarket billing system project report..pdf
Supermarket billing system project report..pdfSupermarket billing system project report..pdf
Supermarket billing system project report..pdf
 
5G and 6G refer to generations of mobile network technology, each representin...
5G and 6G refer to generations of mobile network technology, each representin...5G and 6G refer to generations of mobile network technology, each representin...
5G and 6G refer to generations of mobile network technology, each representin...
 
Online book store management system project.pdf
Online book store management system project.pdfOnline book store management system project.pdf
Online book store management system project.pdf
 
RM&IPR M5 notes.pdfResearch Methodolgy & Intellectual Property Rights Series 5
RM&IPR M5 notes.pdfResearch Methodolgy & Intellectual Property Rights Series 5RM&IPR M5 notes.pdfResearch Methodolgy & Intellectual Property Rights Series 5
RM&IPR M5 notes.pdfResearch Methodolgy & Intellectual Property Rights Series 5
 
Intelligent Agents, A discovery on How A Rational Agent Acts
Intelligent Agents, A discovery on How A Rational Agent ActsIntelligent Agents, A discovery on How A Rational Agent Acts
Intelligent Agents, A discovery on How A Rational Agent Acts
 
Circuit Breaker arc phenomenon.pdf engineering
Circuit Breaker arc phenomenon.pdf engineeringCircuit Breaker arc phenomenon.pdf engineering
Circuit Breaker arc phenomenon.pdf engineering
 
Seismic Hazard Assessment Software in Python by Prof. Dr. Costas Sachpazis
Seismic Hazard Assessment Software in Python by Prof. Dr. Costas SachpazisSeismic Hazard Assessment Software in Python by Prof. Dr. Costas Sachpazis
Seismic Hazard Assessment Software in Python by Prof. Dr. Costas Sachpazis
 
Electrostatic field in a coaxial transmission line
Electrostatic field in a coaxial transmission lineElectrostatic field in a coaxial transmission line
Electrostatic field in a coaxial transmission line
 
Introduction to Artificial Intelligence and History of AI
Introduction to Artificial Intelligence and History of AIIntroduction to Artificial Intelligence and History of AI
Introduction to Artificial Intelligence and History of AI
 
NEWLETTER FRANCE HELICES/ SDS SURFACE DRIVES - MAY 2024
NEWLETTER FRANCE HELICES/ SDS SURFACE DRIVES - MAY 2024NEWLETTER FRANCE HELICES/ SDS SURFACE DRIVES - MAY 2024
NEWLETTER FRANCE HELICES/ SDS SURFACE DRIVES - MAY 2024
 
Research Methodolgy & Intellectual Property Rights Series 2
Research Methodolgy & Intellectual Property Rights Series 2Research Methodolgy & Intellectual Property Rights Series 2
Research Methodolgy & Intellectual Property Rights Series 2
 
Introduction to Heat Exchangers: Principle, Types and Applications
Introduction to Heat Exchangers: Principle, Types and ApplicationsIntroduction to Heat Exchangers: Principle, Types and Applications
Introduction to Heat Exchangers: Principle, Types and Applications
 
Software Engineering - Modelling Concepts + Class Modelling + Building the An...
Software Engineering - Modelling Concepts + Class Modelling + Building the An...Software Engineering - Modelling Concepts + Class Modelling + Building the An...
Software Engineering - Modelling Concepts + Class Modelling + Building the An...
 
Activity Planning: Objectives, Project Schedule, Network Planning Model. Time...
Activity Planning: Objectives, Project Schedule, Network Planning Model. Time...Activity Planning: Objectives, Project Schedule, Network Planning Model. Time...
Activity Planning: Objectives, Project Schedule, Network Planning Model. Time...
 

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