SlideShare ist ein Scribd-Unternehmen logo
1 von 20
UNIVERSITY OF LUCKNOW
LUCKNOW
MASTER OF COMPUTER APPLICATION
Presentation on Event
Handling in JAVA
SUBMITTED BY: -
SRAJAN SHUKLA
MASTER OF COMPUTER APPLICATION
MCA(4TH SEM.)
Introduction
 An event can be defined as a signal to the program that something has
happened.
 Events are triggered either by external user actions, such as mouse
movements, button clicks, and keystrokes, or by internal program
activities, such as a timer.
 The program can choose to respond to or ignore an event.
The component that creates an event and fires it is called the source
object or source component.
 An event is an instance of an event class.
 The root class of the event classes is java.util.EventObject.
 We can identify the source object of an event using the getSource()
method in the EventObject class.
 The subclasses of EventObject deal with special types of events, such
as action events, window events, component events, mouse events,
and key events.
The Delegation Event Model
 The delegation event model defines standard and consistent
mechanisms to generate and process events.
Principle:
 A source generates an event and sends it to one or more listeners.
 The listener waits until it receives an event.
 Once an event is received, the listener processes the event and then
returns.
Advantage:
 The application logic that processes events is cleanly separated
from the user interface logic that generates those events.
 A user interface element is able to “delegate” the processing of an
event to a separate piece of code.
 In the delegation event model, listeners must register with a source
in order to receive an event notification.
Event
 An event is an object that describes a state change in a source.
 It can be generated as a consequence of a person interacting with
the elements in a graphical user interface.
 For Example, pressing a button, entering a character via the
keyboard, selecting an item in a list, and clicking the mouse.
 Events may also occur that are not directly caused by interactions
with a user interface.
 For example, an event may be generated when a timer expires, a
counter exceeds a value, a software or hardware failure occurs, or
an operation is completed.
Event Source
 An Event source is an object that generates an event.
 This occurs when the internal state of that object changes in some way.
 Sources may generate more than one type of event.
 A source must register listeners in order for the listeners to receive
notifications about a specific type of event.
 Each type of event has its own registration method.
public void addTypeListener(TypeListener el)
 When an event occurs, all registered listeners are notified and receive a
copy of the event object. This is known as multicasting the event.
 In all cases, notifications are sent only to listeners that register to receive
them.
 Some sources may allow only one listener to register.
public void addTypeListener(TypeListener el) throws
java.util.TooManyListenersException
Event Listener
 A listener is an object that is notified when an event occurs. It has two
major requirements.
 First, it must have been registered with one or more sources to receive
notifications about specific types of events.
 Second, it must implement methods to receive and process these
notifications.
 The methods that receive and process events are defined in a set of
interfaces found in java.awt.event.
 For example, the MouseMotionListener interface defines two methods
to receive notifications when the mouse is dragged or moved.
Action Listener
 Action listeners are most common event handlers to implement.
 An action event occurs, whenever an action is performed by the user.
 We implement an action listener to define what should be done when
an user performs certain operation.
Examples: When the user clicks a button, chooses a menu item,
presses Enter in a text field.
 The result is that an actionPerformed message is sent to all action
listeners that are registered on the relevant component.
To write an Action Listener, follow the steps given below
 Declare an event handler class and specify that the class either
implements an ActionListener interface or extends a class that
implements an ActionListener interface.
For example:
public class MyClass implements ActionListener {
 Register an instance of the event handler class as a listener on one or
more components.
For example:
someComponent.addActionListener(instanceOfMyClass);
 Include code that implements the methods in listener interface.
For example:
public void actionPerformed(ActionEvent e)
{ ...//code that reacts to the action... }
Action Event Class
Method Purpose
String getActionCommand() Returns the string associated with this
action. Most objects that can fire action
events support a method called
setActionCommand that lets you set
this string.
Object getSource() Returns the object that fired the event.
Item Listener Interface
 Item events are fired by components that implement the
ItemSelectable interface.
 Generally, ItemSelectable components maintain on/off state for one or
more items.
 The Swing components that fire item events include buttons like
check boxes, check menu items, toggle buttons and combo boxes etc.
 ItemListener Interface has only one method.
public void itemStateChanged (ItemEvent)
Item Event class
Method Purpose
Object getItem() Returns the component-specific object
associated with the item whose state
changed. Often this is a String
containing the text on the selected
item.
ItemSelectable getItemSelectable()
Returns the component that fired the
item event. You can use this instead of
the getSource method.
int getStateChange()
Returns the new state of the item. The
ItemEvent class defines two states:
SELECTED and DESELECTED.
Key Listener Interface
 Key events indicate when the user is typing at the keyboard.
 Key events are fired by the component with the keyboard focus when
the user presses or releases keyboard keys.
 Notifications are sent about two basic kinds of key events:
 The typing of a Unicode character
 The pressing or releasing of a key on the keyboard
 The first kind of event is called a key-typed event.
 To know when the user types a Unicode character ? whether by pressing one
key such as 'a' or by pressing several keys in sequence ?
 The second kind is either a key-pressed or key-released event.
 To know when the user presses the F1 key, or whether the user pressed the '3'
key on the number pad, you handle key-pressed events.
Methods of Key Listener Interface
Method Purpose
keyTyped(KeyEvent) Called just after the user types a
Unicode character into the listened-
to component.
keyPressed(KeyEvent) Called just after the user presses a
key while the listened-to
component has the focus.
keyReleased(KeyEvent) Called just after the user releases a
key while the listened-to
component has the focus.
Key Event class
Method Purpose
char getKeyChar()
Obtains the Unicode character associated
with this event.
int getKeyCode()
Obtains the key code associated with this
event. The key code identifies the
particular key on the keyboard that the user
pressed or released. For example, VK_A
specifies the key labeled A, and
VK_ESCAPE specifies the Escape key.
boolean isActionKey()
Returns true if the key firing the event is an
action key. Examples of action keys
include Page Up, Caps Lock, the arrow and
function keys.
Mouse Listener Interface
 Mouse events notify when the user uses the mouse (or similar input
device) to interact with a component.
 Mouse events occur when the cursor enters or exits a component's
onscreen area and when the user presses or releases one of the mouse
buttons.
Methods of Mouse Listener Interface
Method Purpose
mouseClicked(MouseEvent) Called just after the user clicks the
listened-to component.
mouseEntered(MouseEvent) Called just after the cursor enters the
bounds of the listened-to component.
mouseExited(MouseEvent) Called just after the cursor exits the
bounds of the listened-to component.
mousePressed(MouseEvent) Called just after the user presses a
mouse button while the cursor is over
the listened-to component.
mouseReleased(MouseEvent) Called just after the user releases a
mouse button after a mouse press over
the listened-to component.
Mouse Event class
Method Purpose
int getClickCount()
Returns the number of quick, consecutive
clicks the user has made (including this
event). For example, returns 2 for a double
click.
int getButton()
Returns which mouse button, if any, has a
changed state. One of the following
constants is returned: NOBUTTON,
BUTTON1, BUTTON2, or BUTTON3.
int getX()
int getY()
Return the (x,y) position at which the event
occurred, relative to the component that
fired the event.
Window Listener Interface
 The listener interface for receiving window events.
 The class that is interested in processing a window event either
implements this interface (and all the methods it contains) or
extends the abstract WindowAdapter class (overriding only the
methods of interest).
 The listener object created from that class is then registered with
a Window using the window's addWindowListener () method.
Methods of Window Listener
Method Purpose
void windowClosing
(WindowEvent e)
Invoked when the user attempts to close the
window from the window's system menu.
void windowOpened
(WindowEvent e)
Invoked the first time a window is made visible.
void windowClosed
(WindowEvent e)
Invoked when a window has been closed as the
result of calling dispose on the window.
void windowIconified
(WindowEvent e)
Invoked when a window is changed from a
normal to a minimized state.
void windowDeiconified(W
indowEvent e)
Invoked when a window is changed from a
minimized to a normal state.
void windowActivated
(WindowEvent e)
Invoked when the Window is set to be the active
Window.
void windowDeactivated(
WindowEvent e)
Invoked when a Window is no longer the active
Window.
THANK YOU!!!!!!

Weitere ähnliche Inhalte

Was ist angesagt?

Java Thread Synchronization
Java Thread SynchronizationJava Thread Synchronization
Java Thread SynchronizationBenj Del Mundo
 
Presentation on-exception-handling
Presentation on-exception-handlingPresentation on-exception-handling
Presentation on-exception-handlingNahian Ahmed
 
tL20 event handling
tL20 event handlingtL20 event handling
tL20 event handlingteach4uin
 
Event Handling in Java
Event Handling in JavaEvent Handling in Java
Event Handling in JavaAyesha Kanwal
 
Exception Handling in JAVA
Exception Handling in JAVAException Handling in JAVA
Exception Handling in JAVASURIT DATTA
 
Java package
Java packageJava package
Java packageCS_GDRCST
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in javaRaghu nath
 
GUI Programming In Java
GUI Programming In JavaGUI Programming In Java
GUI Programming In Javayht4ever
 
Java awt (abstract window toolkit)
Java awt (abstract window toolkit)Java awt (abstract window toolkit)
Java awt (abstract window toolkit)Elizabeth alexander
 
Constructor in java
Constructor in javaConstructor in java
Constructor in javaHitesh Kumar
 
Inheritance in java
Inheritance in javaInheritance in java
Inheritance in javaTech_MX
 

Was ist angesagt? (20)

GUI components in Java
GUI components in JavaGUI components in Java
GUI components in Java
 
Method overriding
Method overridingMethod overriding
Method overriding
 
Java swing
Java swingJava swing
Java swing
 
Java Thread Synchronization
Java Thread SynchronizationJava Thread Synchronization
Java Thread Synchronization
 
JAVA AWT
JAVA AWTJAVA AWT
JAVA AWT
 
Presentation on-exception-handling
Presentation on-exception-handlingPresentation on-exception-handling
Presentation on-exception-handling
 
tL20 event handling
tL20 event handlingtL20 event handling
tL20 event handling
 
Event Handling in Java
Event Handling in JavaEvent Handling in Java
Event Handling in Java
 
JDBC – Java Database Connectivity
JDBC – Java Database ConnectivityJDBC – Java Database Connectivity
JDBC – Java Database Connectivity
 
Exception Handling in JAVA
Exception Handling in JAVAException Handling in JAVA
Exception Handling in JAVA
 
Java awt
Java awtJava awt
Java awt
 
Java package
Java packageJava package
Java package
 
Java: GUI
Java: GUIJava: GUI
Java: GUI
 
Java interfaces
Java interfacesJava interfaces
Java interfaces
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in java
 
GUI Programming In Java
GUI Programming In JavaGUI Programming In Java
GUI Programming In Java
 
Java awt (abstract window toolkit)
Java awt (abstract window toolkit)Java awt (abstract window toolkit)
Java awt (abstract window toolkit)
 
Constructor in java
Constructor in javaConstructor in java
Constructor in java
 
Inheritance in java
Inheritance in javaInheritance in java
Inheritance in java
 
Applets
AppletsApplets
Applets
 

Ähnlich wie Event Handling in JAVA

Ajp notes-chapter-03
Ajp notes-chapter-03Ajp notes-chapter-03
Ajp notes-chapter-03Ankit Dubey
 
Dr Jammi Ashok - Introduction to Java Material (OOPs)
 Dr Jammi Ashok - Introduction to Java Material (OOPs) Dr Jammi Ashok - Introduction to Java Material (OOPs)
Dr Jammi Ashok - Introduction to Java Material (OOPs)jammiashok123
 
Advance Java Programming(CM5I) Event handling
Advance Java Programming(CM5I) Event handlingAdvance Java Programming(CM5I) Event handling
Advance Java Programming(CM5I) Event handlingPayal Dungarwal
 
Unit-3 event handling
Unit-3 event handlingUnit-3 event handling
Unit-3 event handlingAmol Gaikwad
 
Java gui event
Java gui eventJava gui event
Java gui eventSoftNutx
 
Chapter 11.5
Chapter 11.5Chapter 11.5
Chapter 11.5sotlsoc
 
event-handling.pptx
event-handling.pptxevent-handling.pptx
event-handling.pptxusvirat1805
 
engineeringdsgtnotesofunitfivesnists.ppt
engineeringdsgtnotesofunitfivesnists.pptengineeringdsgtnotesofunitfivesnists.ppt
engineeringdsgtnotesofunitfivesnists.pptsharanyak0721
 
Graphical User Interface (GUI) - 2
Graphical User Interface (GUI) - 2Graphical User Interface (GUI) - 2
Graphical User Interface (GUI) - 2PRN USM
 
event handling new.ppt
event handling new.pptevent handling new.ppt
event handling new.pptusama537223
 
Java Abstract Window Toolkit (AWT) Presentation. 2024
Java Abstract Window Toolkit (AWT) Presentation. 2024Java Abstract Window Toolkit (AWT) Presentation. 2024
Java Abstract Window Toolkit (AWT) Presentation. 2024nehakumari0xf
 
Java Abstract Window Toolkit (AWT) Presentation. 2024
Java Abstract Window Toolkit (AWT) Presentation. 2024Java Abstract Window Toolkit (AWT) Presentation. 2024
Java Abstract Window Toolkit (AWT) Presentation. 2024kashyapneha2809
 

Ähnlich wie Event Handling in JAVA (20)

Ajp notes-chapter-03
Ajp notes-chapter-03Ajp notes-chapter-03
Ajp notes-chapter-03
 
Unit 6 Java
Unit 6 JavaUnit 6 Java
Unit 6 Java
 
Dr Jammi Ashok - Introduction to Java Material (OOPs)
 Dr Jammi Ashok - Introduction to Java Material (OOPs) Dr Jammi Ashok - Introduction to Java Material (OOPs)
Dr Jammi Ashok - Introduction to Java Material (OOPs)
 
What is Event
What is EventWhat is Event
What is Event
 
Advance Java Programming(CM5I) Event handling
Advance Java Programming(CM5I) Event handlingAdvance Java Programming(CM5I) Event handling
Advance Java Programming(CM5I) Event handling
 
Unit-3 event handling
Unit-3 event handlingUnit-3 event handling
Unit-3 event handling
 
Java gui event
Java gui eventJava gui event
Java gui event
 
Chapter 11.5
Chapter 11.5Chapter 11.5
Chapter 11.5
 
event-handling.pptx
event-handling.pptxevent-handling.pptx
event-handling.pptx
 
engineeringdsgtnotesofunitfivesnists.ppt
engineeringdsgtnotesofunitfivesnists.pptengineeringdsgtnotesofunitfivesnists.ppt
engineeringdsgtnotesofunitfivesnists.ppt
 
Graphical User Interface (GUI) - 2
Graphical User Interface (GUI) - 2Graphical User Interface (GUI) - 2
Graphical User Interface (GUI) - 2
 
event handling new.ppt
event handling new.pptevent handling new.ppt
event handling new.ppt
 
Java Abstract Window Toolkit (AWT) Presentation. 2024
Java Abstract Window Toolkit (AWT) Presentation. 2024Java Abstract Window Toolkit (AWT) Presentation. 2024
Java Abstract Window Toolkit (AWT) Presentation. 2024
 
Java Abstract Window Toolkit (AWT) Presentation. 2024
Java Abstract Window Toolkit (AWT) Presentation. 2024Java Abstract Window Toolkit (AWT) Presentation. 2024
Java Abstract Window Toolkit (AWT) Presentation. 2024
 
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
 
event_handling.ppt
event_handling.pptevent_handling.ppt
event_handling.ppt
 
Androd Listeners
Androd ListenersAndrod Listeners
Androd Listeners
 
Module 5.pptx
Module 5.pptxModule 5.pptx
Module 5.pptx
 
Module3.11.pptx
Module3.11.pptxModule3.11.pptx
Module3.11.pptx
 
Swing
SwingSwing
Swing
 

Kürzlich hochgeladen

Karra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptxKarra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptxAshokKarra1
 
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONTHEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONHumphrey A Beña
 
Gas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxGas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxDr.Ibrahim Hassaan
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxiammrhaywood
 
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...Nguyen Thanh Tu Collection
 
Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Mark Reed
 
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxMULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxAnupkumar Sharma
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Celine George
 
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSGRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSJoshuaGantuangco2
 
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)lakshayb543
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatYousafMalik24
 
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Celine George
 
4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptxmary850239
 
How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17Celine George
 
Grade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptxGrade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptxChelloAnnAsuncion2
 
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfLike-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfMr Bounab Samir
 
ENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choomENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choomnelietumpap1
 
Barangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptxBarangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptxCarlos105
 

Kürzlich hochgeladen (20)

Karra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptxKarra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptx
 
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONTHEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
 
Gas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxGas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptx
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
 
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
 
Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)
 
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxMULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17
 
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSGRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
 
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice great
 
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
 
4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx
 
How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17
 
Grade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptxGrade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptx
 
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfLike-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
 
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptxFINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
 
ENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choomENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choom
 
Barangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptxBarangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptx
 
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptxYOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
 

Event Handling in JAVA

  • 1. UNIVERSITY OF LUCKNOW LUCKNOW MASTER OF COMPUTER APPLICATION Presentation on Event Handling in JAVA SUBMITTED BY: - SRAJAN SHUKLA MASTER OF COMPUTER APPLICATION MCA(4TH SEM.)
  • 2. Introduction  An event can be defined as a signal to the program that something has happened.  Events are triggered either by external user actions, such as mouse movements, button clicks, and keystrokes, or by internal program activities, such as a timer.  The program can choose to respond to or ignore an event. The component that creates an event and fires it is called the source object or source component.  An event is an instance of an event class.  The root class of the event classes is java.util.EventObject.  We can identify the source object of an event using the getSource() method in the EventObject class.  The subclasses of EventObject deal with special types of events, such as action events, window events, component events, mouse events, and key events.
  • 3. The Delegation Event Model  The delegation event model defines standard and consistent mechanisms to generate and process events. Principle:  A source generates an event and sends it to one or more listeners.  The listener waits until it receives an event.  Once an event is received, the listener processes the event and then returns. Advantage:  The application logic that processes events is cleanly separated from the user interface logic that generates those events.  A user interface element is able to “delegate” the processing of an event to a separate piece of code.  In the delegation event model, listeners must register with a source in order to receive an event notification.
  • 4. Event  An event is an object that describes a state change in a source.  It can be generated as a consequence of a person interacting with the elements in a graphical user interface.  For Example, pressing a button, entering a character via the keyboard, selecting an item in a list, and clicking the mouse.  Events may also occur that are not directly caused by interactions with a user interface.  For example, an event may be generated when a timer expires, a counter exceeds a value, a software or hardware failure occurs, or an operation is completed.
  • 5. Event Source  An Event source is an object that generates an event.  This occurs when the internal state of that object changes in some way.  Sources may generate more than one type of event.  A source must register listeners in order for the listeners to receive notifications about a specific type of event.  Each type of event has its own registration method. public void addTypeListener(TypeListener el)  When an event occurs, all registered listeners are notified and receive a copy of the event object. This is known as multicasting the event.  In all cases, notifications are sent only to listeners that register to receive them.  Some sources may allow only one listener to register. public void addTypeListener(TypeListener el) throws java.util.TooManyListenersException
  • 6. Event Listener  A listener is an object that is notified when an event occurs. It has two major requirements.  First, it must have been registered with one or more sources to receive notifications about specific types of events.  Second, it must implement methods to receive and process these notifications.  The methods that receive and process events are defined in a set of interfaces found in java.awt.event.  For example, the MouseMotionListener interface defines two methods to receive notifications when the mouse is dragged or moved.
  • 7. Action Listener  Action listeners are most common event handlers to implement.  An action event occurs, whenever an action is performed by the user.  We implement an action listener to define what should be done when an user performs certain operation. Examples: When the user clicks a button, chooses a menu item, presses Enter in a text field.  The result is that an actionPerformed message is sent to all action listeners that are registered on the relevant component.
  • 8. To write an Action Listener, follow the steps given below  Declare an event handler class and specify that the class either implements an ActionListener interface or extends a class that implements an ActionListener interface. For example: public class MyClass implements ActionListener {  Register an instance of the event handler class as a listener on one or more components. For example: someComponent.addActionListener(instanceOfMyClass);  Include code that implements the methods in listener interface. For example: public void actionPerformed(ActionEvent e) { ...//code that reacts to the action... }
  • 9. Action Event Class Method Purpose String getActionCommand() Returns the string associated with this action. Most objects that can fire action events support a method called setActionCommand that lets you set this string. Object getSource() Returns the object that fired the event.
  • 10. Item Listener Interface  Item events are fired by components that implement the ItemSelectable interface.  Generally, ItemSelectable components maintain on/off state for one or more items.  The Swing components that fire item events include buttons like check boxes, check menu items, toggle buttons and combo boxes etc.  ItemListener Interface has only one method. public void itemStateChanged (ItemEvent)
  • 11. Item Event class Method Purpose Object getItem() Returns the component-specific object associated with the item whose state changed. Often this is a String containing the text on the selected item. ItemSelectable getItemSelectable() Returns the component that fired the item event. You can use this instead of the getSource method. int getStateChange() Returns the new state of the item. The ItemEvent class defines two states: SELECTED and DESELECTED.
  • 12. Key Listener Interface  Key events indicate when the user is typing at the keyboard.  Key events are fired by the component with the keyboard focus when the user presses or releases keyboard keys.  Notifications are sent about two basic kinds of key events:  The typing of a Unicode character  The pressing or releasing of a key on the keyboard  The first kind of event is called a key-typed event.  To know when the user types a Unicode character ? whether by pressing one key such as 'a' or by pressing several keys in sequence ?  The second kind is either a key-pressed or key-released event.  To know when the user presses the F1 key, or whether the user pressed the '3' key on the number pad, you handle key-pressed events.
  • 13. Methods of Key Listener Interface Method Purpose keyTyped(KeyEvent) Called just after the user types a Unicode character into the listened- to component. keyPressed(KeyEvent) Called just after the user presses a key while the listened-to component has the focus. keyReleased(KeyEvent) Called just after the user releases a key while the listened-to component has the focus.
  • 14. Key Event class Method Purpose char getKeyChar() Obtains the Unicode character associated with this event. int getKeyCode() Obtains the key code associated with this event. The key code identifies the particular key on the keyboard that the user pressed or released. For example, VK_A specifies the key labeled A, and VK_ESCAPE specifies the Escape key. boolean isActionKey() Returns true if the key firing the event is an action key. Examples of action keys include Page Up, Caps Lock, the arrow and function keys.
  • 15. Mouse Listener Interface  Mouse events notify when the user uses the mouse (or similar input device) to interact with a component.  Mouse events occur when the cursor enters or exits a component's onscreen area and when the user presses or releases one of the mouse buttons.
  • 16. Methods of Mouse Listener Interface Method Purpose mouseClicked(MouseEvent) Called just after the user clicks the listened-to component. mouseEntered(MouseEvent) Called just after the cursor enters the bounds of the listened-to component. mouseExited(MouseEvent) Called just after the cursor exits the bounds of the listened-to component. mousePressed(MouseEvent) Called just after the user presses a mouse button while the cursor is over the listened-to component. mouseReleased(MouseEvent) Called just after the user releases a mouse button after a mouse press over the listened-to component.
  • 17. Mouse Event class Method Purpose int getClickCount() Returns the number of quick, consecutive clicks the user has made (including this event). For example, returns 2 for a double click. int getButton() Returns which mouse button, if any, has a changed state. One of the following constants is returned: NOBUTTON, BUTTON1, BUTTON2, or BUTTON3. int getX() int getY() Return the (x,y) position at which the event occurred, relative to the component that fired the event.
  • 18. Window Listener Interface  The listener interface for receiving window events.  The class that is interested in processing a window event either implements this interface (and all the methods it contains) or extends the abstract WindowAdapter class (overriding only the methods of interest).  The listener object created from that class is then registered with a Window using the window's addWindowListener () method.
  • 19. Methods of Window Listener Method Purpose void windowClosing (WindowEvent e) Invoked when the user attempts to close the window from the window's system menu. void windowOpened (WindowEvent e) Invoked the first time a window is made visible. void windowClosed (WindowEvent e) Invoked when a window has been closed as the result of calling dispose on the window. void windowIconified (WindowEvent e) Invoked when a window is changed from a normal to a minimized state. void windowDeiconified(W indowEvent e) Invoked when a window is changed from a minimized to a normal state. void windowActivated (WindowEvent e) Invoked when the Window is set to be the active Window. void windowDeactivated( WindowEvent e) Invoked when a Window is no longer the active Window.