SlideShare a Scribd company logo
1 of 16
JAVA
EventHandling
Prepared by
Miss. Arati A. Gadgil
EventHandling
Change in the state of an object is known as event i.e. event
describes the change in state of source. Events are generated as
result of user interaction with the graphical user interface
components.
Foreground Events - Those events which require the direct
interaction of user
Background Events - Those events that require the interaction
of end user are known as background events
2
Source - The source is an object on which event occurs
Listener - It is also known as event handler. Listener is
responsible for generating response to an event.
Steps involved in event handling
•The User clicks the button and the event is generated.
•Now the object of concerned event class is created automatically and
information about the source and the event get populated with in
same object.
•Event object is forwarded to the method of registered listener class.
•The method is now get executed and returns. 3
4
Event Class Hierarchy
Each high level event class extends java.awt.AWTEvent.
java.lang.Object
| ---java.util.EventObject
| ---java.awt.AWTEvent
| ---java.awt.event.ActionEvent
| ---java.awt.event.ItemEvent
| ---java.awt.event.AdjustmentEvent
| ---java.awt.event.TextEvent
| ----java.awt.event.ComponentEvent
| ---java.awt.event.InputEvent |
| | ---java.awt.event.KeyEvent
| | ---java.awt.event.MouseEvent
| +---java.awt.event.FocusEvent
| +---java.awt.event.ContainerEvent
| +---java.awt.event.WindowEvent
5
Semantic and Low-Level Events in the AWT
A semantic event is one that expresses what the user is doing, such as
"clicking that button"; hence, an ActionEvent is a semantic event.
Low-level events are those events that make this possible. In the case of
a button click, this is a mouse down, a series of mouse moves, and a
mouse up (but only if the mouse up is inside the button area). Or it might
be a keystroke, which happens if the user selects the button with
the TAB key and then activates it with the space bar. Similarly, adjusting
a scrollbar is a semantic event, but dragging the mouse is a low-level
event.
Semantic event classes in the java.awt.event package:
ActionEvent (for a button click, a menu selection, selecting a list item,
or ENTER typed in a text field)
AdjustmentEvent (the user adjusted a scrollbar)
ItemEvent (the user made a selection from a set of checkbox or list
6
Five low-level event classes are commonly used:
•KeyEvent (a key was pressed or released)
•MouseEvent (the mouse button was pressed, released, moved, or
dragged)
•MouseWheelEvent (the mouse wheel was rotated)
•FocusEvent (a component got focus, or lost focus). See page 321 for
more information about the focus concept.
•WindowEvent (the window state changed)
AWT ActionListener Interface
Method
void actionPerformed(ActionEvent e)
Invoked when an action occurs.
The Event listener represent the interfaces
responsible to handle events.
7
AWT MouseListener Interface
Methods
•void mouseClicked(MouseEvent e)
Invoked when the mouse button has been clicked (pressed and
released) on a component.
•void mouseEntered(MouseEvent e)
Invoked when the mouse enters a component
•void mouseExited(MouseEvent e)
Invoked when the mouse exits a component.
•void mousePressed(MouseEvent e)
Invoked when a mouse button has been pressed on a component.
•void mouseReleased(MouseEvent e)
Invoked when a mouse button has been released on a
component.
.
8
AWT WindowListener Interface
Methods
void windowActivated(WindowEvent e)
Invoked when the Window is set to be the active Window
void windowClosed(WindowEvent e)
Invoked when a window has been closed as the result of calling dispose
on the window.
void windowClosing(WindowEvent e)
Invoked when the user attempts to close the window from the window's
system menu.
void windowDeactivated(WindowEvent e)
Invoked when a Window is no longer the active Window
9
void windowDeiconified(WindowEvent e)
Invoked when a window is changed from a
minimized to a normal state
void windowIconified(WindowEvent e)
Invoked when a window is changed from a
normal to a minimized state.
void windowOpened(WindowEvent e)
Invoked the first time a window is made visible.
10
import java.awt.*;
import java.awt.event.*;
class frmw extends Frame implements WindowListener
{
frmw()
{
super("window Listener");
addWindowListener(this);
setSize(300,300);
setVisible(true);
}
public void windowActivated(WindowEvent e)
{
System.out.println("Activated");
}
11
public void windowDeactivated(WindowEvent e)
{
System.out.println("Deactivated");
}
public void windowIconified(WindowEvent e)
{
System.out.println("Iconified");
}
public void windowDeiconified(WindowEvent e)
{
System.out.println("Deiconified");
}
public void windowOpened(WindowEvent e)
{
System.out.println("opened");
} 12
public void windowClosed(WindowEvent e)
{
System.out.println("closed");
}
public void windowClosing(WindowEvent e)
{
System.out.println("closing");
}
public static void main(String []a)
{
frmw k=new frmw();
}
}
13
AWT MouseMotionListener Interface
Methods
void mouseDragged(MouseEvent e)
Invoked when a mouse button is pressed on a component and then
dragged
void mouseMoved(MouseEvent e)
Invoked when the mouse cursor has been moved onto a component
but no buttons have been pushed
14
AWT FocusListener Interface
Methods
void focusGained(FocusEvent e)
Invoked when a component gains the keyboard focus
void focusLost(FocusEvent e)
Invoked when a component loses the keyboard focus
15
Thank You
16

More Related Content

What's hot

android activity
android activityandroid activity
android activity
Deepa Rani
 
Java exception handling
Java exception handlingJava exception handling
Java exception handling
BHUVIJAYAVELU
 

What's hot (20)

Event handling
Event handlingEvent handling
Event handling
 
Event Handling in Java
Event Handling in JavaEvent Handling in Java
Event Handling in Java
 
Basic of Multithreading in JAva
Basic of Multithreading in JAvaBasic of Multithreading in JAva
Basic of Multithreading in JAva
 
Java I/o streams
Java I/o streamsJava I/o streams
Java I/o streams
 
android activity
android activityandroid activity
android activity
 
Event handling
Event handlingEvent handling
Event handling
 
Java And Multithreading
Java And MultithreadingJava And Multithreading
Java And Multithreading
 
Java exception handling
Java exception handlingJava exception handling
Java exception handling
 
JAVA PROGRAMMING- GUI Programming with Swing - The Swing Buttons
JAVA PROGRAMMING- GUI Programming with Swing - The Swing ButtonsJAVA PROGRAMMING- GUI Programming with Swing - The Swing Buttons
JAVA PROGRAMMING- GUI Programming with Swing - The Swing Buttons
 
Control Statements in Java
Control Statements in JavaControl Statements in Java
Control Statements in Java
 
Method overriding
Method overridingMethod overriding
Method overriding
 
Classes objects in java
Classes objects in javaClasses objects in java
Classes objects in java
 
Abstract Base Class and Polymorphism in C++
Abstract Base Class and Polymorphism in C++Abstract Base Class and Polymorphism in C++
Abstract Base Class and Polymorphism in C++
 
Method overloading
Method overloadingMethod overloading
Method overloading
 
Awt
AwtAwt
Awt
 
Java swing
Java swingJava swing
Java swing
 
Introduction to oop
Introduction to oopIntroduction to oop
Introduction to oop
 
Java applets
Java appletsJava applets
Java applets
 
Asp.NET Validation controls
Asp.NET Validation controlsAsp.NET Validation controls
Asp.NET Validation controls
 
Threads in JAVA
Threads in JAVAThreads in JAVA
Threads in JAVA
 

Similar to Java eventhandling

ITE 1122_ Event Handling.pptx
ITE 1122_ Event Handling.pptxITE 1122_ Event Handling.pptx
ITE 1122_ Event Handling.pptx
udithaisur
 

Similar to Java eventhandling (20)

Lecture 18
Lecture 18Lecture 18
Lecture 18
 
Java Event Handling
Java Event HandlingJava Event Handling
Java Event Handling
 
What is Event
What is EventWhat is Event
What is Event
 
engineeringdsgtnotesofunitfivesnists.ppt
engineeringdsgtnotesofunitfivesnists.pptengineeringdsgtnotesofunitfivesnists.ppt
engineeringdsgtnotesofunitfivesnists.ppt
 
tL20 event handling
tL20 event handlingtL20 event handling
tL20 event handling
 
event-handling.pptx
event-handling.pptxevent-handling.pptx
event-handling.pptx
 
Ajp notes-chapter-03
Ajp notes-chapter-03Ajp notes-chapter-03
Ajp notes-chapter-03
 
ITE 1122_ Event Handling.pptx
ITE 1122_ Event Handling.pptxITE 1122_ Event Handling.pptx
ITE 1122_ Event Handling.pptx
 
Java gui event
Java gui eventJava gui event
Java gui event
 
Event Handling in JAVA
Event Handling in JAVAEvent Handling in JAVA
Event Handling in JAVA
 
event-handling.pptx
event-handling.pptxevent-handling.pptx
event-handling.pptx
 
09events
09events09events
09events
 
Mobile Application Development
Mobile Application DevelopmentMobile Application Development
Mobile Application Development
 
Event handling in Java(part 1)
Event handling in Java(part 1)Event handling in Java(part 1)
Event handling in Java(part 1)
 
Unit 6 Java
Unit 6 JavaUnit 6 Java
Unit 6 Java
 
event handling new.ppt
event handling new.pptevent handling new.ppt
event handling new.ppt
 
Chap - 2 - Event Handling.pptx
Chap - 2 - Event Handling.pptxChap - 2 - Event Handling.pptx
Chap - 2 - Event Handling.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
 
File Handling
File HandlingFile Handling
File Handling
 
Jp notes
Jp notesJp notes
Jp notes
 

More from Arati Gadgil

More from Arati Gadgil (15)

Java adapter
Java adapterJava adapter
Java adapter
 
Java swing
Java swingJava swing
Java swing
 
Java applet
Java appletJava applet
Java applet
 
Java layoutmanager
Java layoutmanagerJava layoutmanager
Java layoutmanager
 
Java stream
Java streamJava stream
Java stream
 
Java thread
Java threadJava thread
Java thread
 
Java networking
Java networkingJava networking
Java networking
 
Java jdbc
Java jdbcJava jdbc
Java jdbc
 
Java package
Java packageJava package
Java package
 
Java interface
Java interfaceJava interface
Java interface
 
Java inheritance
Java inheritanceJava inheritance
Java inheritance
 
Java exception
Java exception Java exception
Java exception
 
Java collection
Java collectionJava collection
Java collection
 
Java class
Java classJava class
Java class
 
Java basic
Java basicJava basic
Java basic
 

Recently uploaded

Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdfVishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdf
ssuserdda66b
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
QucHHunhnh
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
heathfieldcps1
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
KarakKing
 

Recently uploaded (20)

Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdfVishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdf
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
Spatium Project Simulation student brief
Spatium Project Simulation student briefSpatium Project Simulation student brief
Spatium Project Simulation student brief
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
Dyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptxDyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptx
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptx
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structure
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdf
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptx
 

Java eventhandling

  • 2. EventHandling Change in the state of an object is known as event i.e. event describes the change in state of source. Events are generated as result of user interaction with the graphical user interface components. Foreground Events - Those events which require the direct interaction of user Background Events - Those events that require the interaction of end user are known as background events 2
  • 3. Source - The source is an object on which event occurs Listener - It is also known as event handler. Listener is responsible for generating response to an event. Steps involved in event handling •The User clicks the button and the event is generated. •Now the object of concerned event class is created automatically and information about the source and the event get populated with in same object. •Event object is forwarded to the method of registered listener class. •The method is now get executed and returns. 3
  • 4. 4 Event Class Hierarchy Each high level event class extends java.awt.AWTEvent. java.lang.Object | ---java.util.EventObject | ---java.awt.AWTEvent | ---java.awt.event.ActionEvent | ---java.awt.event.ItemEvent | ---java.awt.event.AdjustmentEvent | ---java.awt.event.TextEvent | ----java.awt.event.ComponentEvent | ---java.awt.event.InputEvent | | | ---java.awt.event.KeyEvent | | ---java.awt.event.MouseEvent | +---java.awt.event.FocusEvent | +---java.awt.event.ContainerEvent | +---java.awt.event.WindowEvent
  • 5. 5 Semantic and Low-Level Events in the AWT A semantic event is one that expresses what the user is doing, such as "clicking that button"; hence, an ActionEvent is a semantic event. Low-level events are those events that make this possible. In the case of a button click, this is a mouse down, a series of mouse moves, and a mouse up (but only if the mouse up is inside the button area). Or it might be a keystroke, which happens if the user selects the button with the TAB key and then activates it with the space bar. Similarly, adjusting a scrollbar is a semantic event, but dragging the mouse is a low-level event. Semantic event classes in the java.awt.event package: ActionEvent (for a button click, a menu selection, selecting a list item, or ENTER typed in a text field) AdjustmentEvent (the user adjusted a scrollbar) ItemEvent (the user made a selection from a set of checkbox or list
  • 6. 6 Five low-level event classes are commonly used: •KeyEvent (a key was pressed or released) •MouseEvent (the mouse button was pressed, released, moved, or dragged) •MouseWheelEvent (the mouse wheel was rotated) •FocusEvent (a component got focus, or lost focus). See page 321 for more information about the focus concept. •WindowEvent (the window state changed)
  • 7. AWT ActionListener Interface Method void actionPerformed(ActionEvent e) Invoked when an action occurs. The Event listener represent the interfaces responsible to handle events. 7
  • 8. AWT MouseListener Interface Methods •void mouseClicked(MouseEvent e) Invoked when the mouse button has been clicked (pressed and released) on a component. •void mouseEntered(MouseEvent e) Invoked when the mouse enters a component •void mouseExited(MouseEvent e) Invoked when the mouse exits a component. •void mousePressed(MouseEvent e) Invoked when a mouse button has been pressed on a component. •void mouseReleased(MouseEvent e) Invoked when a mouse button has been released on a component. . 8
  • 9. AWT WindowListener Interface Methods void windowActivated(WindowEvent e) Invoked when the Window is set to be the active Window void windowClosed(WindowEvent e) Invoked when a window has been closed as the result of calling dispose on the window. void windowClosing(WindowEvent e) Invoked when the user attempts to close the window from the window's system menu. void windowDeactivated(WindowEvent e) Invoked when a Window is no longer the active Window 9
  • 10. void windowDeiconified(WindowEvent e) Invoked when a window is changed from a minimized to a normal state void windowIconified(WindowEvent e) Invoked when a window is changed from a normal to a minimized state. void windowOpened(WindowEvent e) Invoked the first time a window is made visible. 10
  • 11. import java.awt.*; import java.awt.event.*; class frmw extends Frame implements WindowListener { frmw() { super("window Listener"); addWindowListener(this); setSize(300,300); setVisible(true); } public void windowActivated(WindowEvent e) { System.out.println("Activated"); } 11
  • 12. public void windowDeactivated(WindowEvent e) { System.out.println("Deactivated"); } public void windowIconified(WindowEvent e) { System.out.println("Iconified"); } public void windowDeiconified(WindowEvent e) { System.out.println("Deiconified"); } public void windowOpened(WindowEvent e) { System.out.println("opened"); } 12
  • 13. public void windowClosed(WindowEvent e) { System.out.println("closed"); } public void windowClosing(WindowEvent e) { System.out.println("closing"); } public static void main(String []a) { frmw k=new frmw(); } } 13
  • 14. AWT MouseMotionListener Interface Methods void mouseDragged(MouseEvent e) Invoked when a mouse button is pressed on a component and then dragged void mouseMoved(MouseEvent e) Invoked when the mouse cursor has been moved onto a component but no buttons have been pushed 14
  • 15. AWT FocusListener Interface Methods void focusGained(FocusEvent e) Invoked when a component gains the keyboard focus void focusLost(FocusEvent e) Invoked when a component loses the keyboard focus 15