SlideShare a Scribd company logo
1 of 30
Module 3.1
https://classroom.google.com/c/NTAyMjQzMzE5OTQ3/m/NTg1
ODY1MzkyNTQ1/details
Two event handling mechanisms; The delegation event model; Event
classes; Sources of events; Event listener interfaces; Using the delegation
event model
0
1
Event Handling
The Delegation Event Model
1) a source generates an event and sends it to one or more listeners.
2) In this scheme, the listener simply waits until it receives an event.
3) Once an event is received, the listener processes the event and then returns.
• The advantage of this design is that 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.
Events
• an event is an object that describes a state change in a source.
• Events may also occur that are not directly caused by interactions with a user interface.
The Delegation Event Model cont…
Event Sources
• A source is an object that generates an event. This occurs when the internal state of that
object changes in some way
• A source must register listeners in order for the listeners to receive notifications about a
specific type of event.
• Here is the general form:
public void addTypeListener(TypeListener el)
• Here, Type is the name of the event, and el is a reference to the event listener.
• When an event occurs, all registered listeners are notified and receive a copy of the event
object. This is known as multicasting the event.
• Some sources may allow only one listener to register.
public void addTypeListener(TypeListener el) throws java.util.TooManyListenersException
Cont…
• A source must also provide a method that allows a listener to unregister an interest in a
specific type of event.
public void removeTypeListener(TypeListener el)
Event Listeners
• 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.
Event Classes
• The classes that represent events are at the core of Java’s event handling mechanism.
• The most widely used events are those defined by the AWT and those defined by Swing.
• At the root of the Java event class hierarchy is EventObject, which is in java.util.
• The class AWTEvent, defined within the java.awt package, is a subclass of EventObject.
Event Classes cont…
The ActionEvent Class
• An ActionEvent is generated when a button is pressed, a list item is double-clicked, or a
menu item is selected.
• ActionEvent has these three constructors:
ActionEvent(Object src, int type, String cmd)
ActionEvent(Object src, int type, String cmd, int modifiers)
ActionEvent(Object src, int type, String cmd, long when, int modifiers)
• You can obtain the command name for the invoking ActionEvent object by using the
getActionCommand( ) method, shown here:
String getActionCommand( )
• The getModifiers( ) method returns a value that indicates which modifier keys (ALT, CTRL,
META, and/or SHIFT) were pressed when the event was generated.
The AdjustmentEvent Class
• An AdjustmentEvent is generated by a scroll bar. There are five types of adjustment events.
• The AdjustmentEvent class defines integer constants that can be used to identify them. The
constants and their meanings are shown here:
BLOCK_DECREMENT= The user clicked inside the scroll bar to decrease its value.
BLOCK_INCREMENT= The user clicked inside the scroll bar to increase its value.
TRACK =The slider was dragged.
UNIT_DECREMENT =The button at the end of the scroll bar was clicked to decrease
its value.
UNIT_INCREMENT =The button at the end of the scroll bar was clicked to increase
its value.
AdjustmentEvent(Adjustable src, int id, int type, int data)
• Here, src is a reference to the object that generated this event. The id specifies the event. The
type of the adjustment is specified by type, and its associated data is data.
The ComponentEvent Class
• A ComponentEvent is generated when the size, position, or visibility of a component is
changed.
• There are four types of component events
COMPONENT_HIDDEN = The component was hidden.
COMPONENT_MOVED = The component was moved.
COMPONENT_RESIZED = The component was resized.
COMPONENT_SHOWN = The component became visible.
ComponentEvent(Component src, int type)
• src = is a reference to the object that generated this event.
• type = The type of the event is specified.
The ContainerEvent Class
• AContainerEvent is generated when a component is added to or removed from a
container.
• There are two types of container events.
COMPONENT_ADDED and COMPONENT_REMOVED
ContainerEvent(Component src, int type, Component comp)
• src = reference to the container that generated this event.
• type = The type of the event is specified
• comp = the component that has been added to or removed from the container.
The FocusEvent Class
• The FocusEvent Class A FocusEvent is generated when a component gains or loses input focus.
• These events are identified by the integer constants FOCUS_GAINED and FOCUS_LOST.
• FocusEvent is a subclass of ComponentEvent and has these constructors:
FocusEvent(Component src, int type)
FocusEvent(Component src, int type, boolean temporaryFlag)
FocusEvent(Component src, int type, boolean temporaryFlag, Component other)
• src = reference to the component that generated this event.
• type = The type of the event is specified by.
• temporaryFlag = temporaryFlag is set to true if the focus event is temporary. Otherwise, it is set
to false.
• The other component involved in the focus change, called the opposite component, is passed in
other. Therefore, if a FOCUS_GAINED event occurred, other will refer to the component that lost
focus. Conversely, if a FOCUS_LOST event occurred, other will refer to the component that gains
focus.
The InputEvent Class
• The InputEvent Class is the abstract class. InputEvent is a subclass of ComponentEvent
• InputEvent defines several integer constants that represent any modifiers, such as the
control key being pressed, that might be associated with the event.
• Its subclasses are KeyEvent and MouseEvent.
• To test if a modifier was pressed at the time an event is generated
boolean isControlDown( )
boolean isMetaDown( )
boolean isAltDown( )
boolean isAltGraphDown( )
boolean isShiftDown( )
The ItemEvent Class
• An ItemEvent is generated when a check box or a list item is clicked or when a checkable
menu item is selected or deselected.
• There are two types of item events, which are identified by the following integer
constants:
DESELECTED The user deselected an item.
SELECTED The user selected an item.
ItemEvent(ItemSelectable src, int type, Object entry, int state)
• src = reference to the component that generated this event.
• type = The type of the event is specified by.
• entry = The specific item that generated the item event is passed .
• State = The current state of that item.
The KeyEvent Class
• A KeyEvent is generated when keyboard input occurs. There are three types of key events, which
are identified by these integer constants: KEY_PRESSED, KEY_RELEASED, and KEY_TYPED.
• There are many other integer constants that are defined by KeyEvent. For example, VK_0 through
VK_9 and VK_A through VK_Z define the ASCII equivalents of the numbers and letters.
KeyEvent(Component src, int type, long when, int modifiers, int code, char ch)
• src = reference to the component that generated the event.
• type = type of the event is specified by
• when = The system time at which the key was pressed is passed .
• modifiers = indicates which modifiers were pressed when this key event occurred.
• code = virtual key code, such as VK_UP, VK_A, and so forth, is passed in.
• ch = The character equivalent (if one exists) is passed in. If no valid character exists, then ch
contains CHAR_UNDEFINED.
The MouseEvent Class
• There are eight types of mouse events.
MOUSE_CLICKED The user clicked the mouse.
MOUSE_DRAGGED The user dragged the mouse.
MOUSE_ENTERED The mouse entered a component.
MOUSE_EXITED The mouse exited from a component.
MOUSE_MOVED The mouse moved.
MOUSE_PRESSED The mouse was pressed.
MOUSE_RELEASED The mouse was released.
MOUSE_WHEEL The mouse wheel was moved.
MouseEvent(Component src, int type, long when, int modifiers, int x, int y, int clicks,
boolean triggersPopup)
The MouseWheelEvent Class
• The MouseWheelEvent class encapsulates a mouse wheel event. It is a subclass of
MouseEvent.
• Mouse wheels are used for scrolling. MouseWheelEvent defines these two integer
constants:
WHEEL_BLOCK_SCROLL
WHEEL_UNIT_SCROLL
= A page-up or page-down scroll event occurred.
=A line-up or line-down scroll event occurred.
MouseWheelEvent(Component src, int type, long when, int modifiers, int x, int y, int clicks,
boolean triggersPopup, int scrollHow, int amount, int count)
The TextEvent Class
• Instances of this class describe text events. These are generated by text fields and text
areas when characters are entered by a user or Programram.
TextEvent(Object src, int type)
The WindowEvent Class
WINDOW_ACTIVATED
WINDOW_CLOSING
WINDOW_DEICONIFIED
WINDOW_ICONIFIED
WINDOW_OPENED
WINDOW_CLOSED
WINDOW_DEACTIVATED
WINDOW_GAINED_FOCUS
WINDOW_LOST_FOCUS
WINDOW_STATE_CHANGED
The WindowEvent Class cont…
• The next three constructors offer more detailed control:
WindowEvent(Window src, int type, Window other)
WindowEvent(Window src, int type, int fromState, int toState)
WindowEvent(Window src, int type, Window other, int fromState, int toState)
Sources of Events
Button
Choice
Menu
Text
Check box
List
Scroll bar
Window
Event Listener Interfaces
• the delegation event model has
two parts: sources and listeners.
• Listeners are created by
implementing one or more of
the interfaces defined by the
java.awt.event package.
The AdjustmentListener Interface
• This interface defines the adjustmentValueChanged( ) method that is invoked when an
adjustment event occurs.
• Its general form is shown here:
void adjustmentValueChanged(AdjustmentEvent ae)
The ComponentListener Interface
• This interface defines four methods that are invoked when a component is resized,
moved, shown, or hidden.
• Their general forms are shown here:
void componentResized(ComponentEvent ce)
void componentMoved(ComponentEvent ce)
void componentShown(ComponentEvent ce)
void componentHidden(ComponentEvent ce)
The ContainerListener Interface
• This interface contains two methods. When a component is added to a container,
componentAdded( ) is invoked. When a component is removed from a container,
componentRemoved( ) is invoked.
• Their general forms are shown here:
void componentAdded(ContainerEvent ce)
void componentRemoved(ContainerEvent ce)
• The FocusListener Interface
• This interface defines two methods. When a component obtains keyboard focus,
focusGained( ) is invoked. When a component loses keyboard focus, focusLost( ) is called.
• Their general forms are shown here:
void focusGained(FocusEvent fe) void
focusLost(FocusEvent fe)
The ItemListener Interface
• This interface defines the itemStateChanged( ) method that is invoked when the state of
an item changes.
• Its general form is shown here:
void itemStateChanged(ItemEvent ie)
The KeyListener Interface
• The general forms of these methods are shown here:
void keyPressed(KeyEvent ke)
void keyReleased(KeyEvent ke)
void keyTyped(KeyEvent ke)
The MouseListener Interface
• This interface defines five methods.
• The general forms of these methods are shown here:
void mouseClicked(MouseEvent me)
void mouseEntered(MouseEvent me)
void mouseExited(MouseEvent me)
void mousePressed(MouseEvent me)
void mouseReleased(MouseEvent me)
The MouseMotionListener Interface
• This interface defines two methods.
• Their general forms are shown here:
void mouseDragged(MouseEvent me)
void mouseMoved(MouseEvent me)
The MouseWheelListener Interface
• This interface defines the mouseWheelMoved( ) method that is invoked when the mouse wheel
is moved.
• Its general form is shown here:
void mouseWheelMoved(MouseWheelEvent mwe)
The TextListener Interface
• This interface defines the textChanged( ) method that is invoked when a change occurs in a text
area or text field.
• Its general form is shown here:
void textChanged(TextEvent te)
• The WindowFocusListener Interface
• This interface defines two methods: windowGainedFocus( ) and windowLostFocus( ). These are
called when a window gains or loses input focus.
• Their general forms are shown here:
void windowGainedFocus(WindowEvent we)
void windowLostFocus(WindowEvent we)
The WindowListener Interface
• This interface defines seven methods.
• The general forms of these methods are
void windowActivated(WindowEvent we)
void windowClosed(WindowEvent we)
void windowClosing(WindowEvent we)
void windowDeactivated(WindowEvent we)
void windowDeiconified(WindowEvent we)
void windowIconified(WindowEvent we)
void windowOpened(WindowEvent we)
Using the Delegation Event Model
• Using the delegation event model is actually quite easy. Just follow these two steps:
1. Implement the appropriate interface in the listener so that it will receive the type
of event desired.
2.Implement code to register and unregister (if necessary) the listener as a recipient
for the event notifications.
Handling Mouse Events
• To handle mouse events, you must implement the MouseListener and the
MouseMotionListener interfaces.
Program on page no 654
Event Handling: The Delegation Model

More Related Content

Similar to Event Handling: The Delegation Model

Event Handling in JAVA
Event Handling in JAVAEvent Handling in JAVA
Event Handling in JAVASrajan Shukla
 
event-handling.pptx
event-handling.pptxevent-handling.pptx
event-handling.pptxusvirat1805
 
OOP Lecture 11-EventHandling1.pptx
OOP Lecture 11-EventHandling1.pptxOOP Lecture 11-EventHandling1.pptx
OOP Lecture 11-EventHandling1.pptxTanzila Kehkashan
 
Event Handling in Java
Event Handling in JavaEvent Handling in Java
Event Handling in JavaAyesha Kanwal
 
Event handling
Event handlingEvent handling
Event handlingswapnac12
 
Java event processing model in c# and java
Java  event processing model in c# and javaJava  event processing model in c# and java
Java event processing model in c# and javaTech_MX
 
Java gui event
Java gui eventJava gui event
Java gui eventSoftNutx
 
Swing and Graphical User Interface in Java
Swing and Graphical User Interface in JavaSwing and Graphical User Interface in Java
Swing and Graphical User Interface in Javababak danyal
 
Event handling62
Event handling62Event handling62
Event handling62myrajendra
 
Chap - 2 - Event Handling.pptx
Chap - 2 - Event Handling.pptxChap - 2 - Event Handling.pptx
Chap - 2 - Event Handling.pptxTadeseBeyene
 
File Handling
File HandlingFile Handling
File HandlingSohanur63
 
Java eventhandling
Java eventhandlingJava eventhandling
Java eventhandlingArati Gadgil
 
event-handling.pptx
event-handling.pptxevent-handling.pptx
event-handling.pptxGood657694
 
engineeringdsgtnotesofunitfivesnists.ppt
engineeringdsgtnotesofunitfivesnists.pptengineeringdsgtnotesofunitfivesnists.ppt
engineeringdsgtnotesofunitfivesnists.pptsharanyak0721
 

Similar to Event Handling: The Delegation Model (20)

Event Handling in JAVA
Event Handling in JAVAEvent Handling in JAVA
Event Handling in JAVA
 
Event handling
Event handlingEvent handling
Event handling
 
event-handling.pptx
event-handling.pptxevent-handling.pptx
event-handling.pptx
 
OOP Lecture 11-EventHandling1.pptx
OOP Lecture 11-EventHandling1.pptxOOP Lecture 11-EventHandling1.pptx
OOP Lecture 11-EventHandling1.pptx
 
Unit 6 Java
Unit 6 JavaUnit 6 Java
Unit 6 Java
 
Event Handling in Java
Event Handling in JavaEvent Handling in Java
Event Handling in Java
 
Event handling
Event handlingEvent handling
Event handling
 
AJP Event classes.pptx
AJP Event classes.pptxAJP Event classes.pptx
AJP Event classes.pptx
 
Java event processing model in c# and java
Java  event processing model in c# and javaJava  event processing model in c# and java
Java event processing model in c# and java
 
Java gui event
Java gui eventJava gui event
Java gui event
 
Swing and Graphical User Interface in Java
Swing and Graphical User Interface in JavaSwing and Graphical User Interface in Java
Swing and Graphical User Interface in Java
 
Event handling62
Event handling62Event handling62
Event handling62
 
Chap - 2 - Event Handling.pptx
Chap - 2 - Event Handling.pptxChap - 2 - Event Handling.pptx
Chap - 2 - Event Handling.pptx
 
File Handling
File HandlingFile Handling
File Handling
 
Java eventhandling
Java eventhandlingJava eventhandling
Java eventhandling
 
What is Event
What is EventWhat is Event
What is Event
 
Event handling in Java(part 1)
Event handling in Java(part 1)Event handling in Java(part 1)
Event handling in Java(part 1)
 
event-handling.pptx
event-handling.pptxevent-handling.pptx
event-handling.pptx
 
engineeringdsgtnotesofunitfivesnists.ppt
engineeringdsgtnotesofunitfivesnists.pptengineeringdsgtnotesofunitfivesnists.ppt
engineeringdsgtnotesofunitfivesnists.ppt
 
09events
09events09events
09events
 

More from VeenaNaik23

Introduction-to-WFS.pptxIntroduction-to-WFS
Introduction-to-WFS.pptxIntroduction-to-WFSIntroduction-to-WFS.pptxIntroduction-to-WFS
Introduction-to-WFS.pptxIntroduction-to-WFSVeenaNaik23
 
Fileoperations.pptx
Fileoperations.pptxFileoperations.pptx
Fileoperations.pptxVeenaNaik23
 
HIV &AIDS AWARENESS 2022-23.pptx
HIV &AIDS AWARENESS 2022-23.pptxHIV &AIDS AWARENESS 2022-23.pptx
HIV &AIDS AWARENESS 2022-23.pptxVeenaNaik23
 

More from VeenaNaik23 (9)

Introduction-to-WFS.pptxIntroduction-to-WFS
Introduction-to-WFS.pptxIntroduction-to-WFSIntroduction-to-WFS.pptxIntroduction-to-WFS
Introduction-to-WFS.pptxIntroduction-to-WFS
 
Fileoperations.pptx
Fileoperations.pptxFileoperations.pptx
Fileoperations.pptx
 
wcmwiki.pptx
wcmwiki.pptxwcmwiki.pptx
wcmwiki.pptx
 
web1.pdf
web1.pdfweb1.pdf
web1.pdf
 
canvas.pptx
canvas.pptxcanvas.pptx
canvas.pptx
 
HIV &AIDS AWARENESS 2022-23.pptx
HIV &AIDS AWARENESS 2022-23.pptxHIV &AIDS AWARENESS 2022-23.pptx
HIV &AIDS AWARENESS 2022-23.pptx
 
Package.pptx
Package.pptxPackage.pptx
Package.pptx
 
Mis
MisMis
Mis
 
unit3.3.pptx
unit3.3.pptxunit3.3.pptx
unit3.3.pptx
 

Recently uploaded

Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...fonyou31
 
social pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajansocial pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajanpragatimahajan3
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104misteraugie
 
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...Sapna Thakur
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Disha Kariya
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeThiyagu K
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDThiyagu K
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Celine George
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingTechSoup
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactPECB
 
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...anjaliyadav012327
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfJayanti Pande
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docxPoojaSen20
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfchloefrazer622
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 

Recently uploaded (20)

Advance Mobile Application Development class 07
Advance Mobile Application Development class 07Advance Mobile Application Development class 07
Advance Mobile Application Development class 07
 
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
 
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
 
social pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajansocial pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajan
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SD
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docx
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdf
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 

Event Handling: The Delegation Model

  • 3. Two event handling mechanisms; The delegation event model; Event classes; Sources of events; Event listener interfaces; Using the delegation event model 0 1
  • 5. The Delegation Event Model 1) a source generates an event and sends it to one or more listeners. 2) In this scheme, the listener simply waits until it receives an event. 3) Once an event is received, the listener processes the event and then returns. • The advantage of this design is that 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. Events • an event is an object that describes a state change in a source. • Events may also occur that are not directly caused by interactions with a user interface.
  • 6. The Delegation Event Model cont… Event Sources • A source is an object that generates an event. This occurs when the internal state of that object changes in some way • A source must register listeners in order for the listeners to receive notifications about a specific type of event. • Here is the general form: public void addTypeListener(TypeListener el) • Here, Type is the name of the event, and el is a reference to the event listener. • When an event occurs, all registered listeners are notified and receive a copy of the event object. This is known as multicasting the event. • Some sources may allow only one listener to register. public void addTypeListener(TypeListener el) throws java.util.TooManyListenersException
  • 7. Cont… • A source must also provide a method that allows a listener to unregister an interest in a specific type of event. public void removeTypeListener(TypeListener el) Event Listeners • 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.
  • 8. Event Classes • The classes that represent events are at the core of Java’s event handling mechanism. • The most widely used events are those defined by the AWT and those defined by Swing. • At the root of the Java event class hierarchy is EventObject, which is in java.util. • The class AWTEvent, defined within the java.awt package, is a subclass of EventObject.
  • 10. The ActionEvent Class • An ActionEvent is generated when a button is pressed, a list item is double-clicked, or a menu item is selected. • ActionEvent has these three constructors: ActionEvent(Object src, int type, String cmd) ActionEvent(Object src, int type, String cmd, int modifiers) ActionEvent(Object src, int type, String cmd, long when, int modifiers) • You can obtain the command name for the invoking ActionEvent object by using the getActionCommand( ) method, shown here: String getActionCommand( ) • The getModifiers( ) method returns a value that indicates which modifier keys (ALT, CTRL, META, and/or SHIFT) were pressed when the event was generated.
  • 11. The AdjustmentEvent Class • An AdjustmentEvent is generated by a scroll bar. There are five types of adjustment events. • The AdjustmentEvent class defines integer constants that can be used to identify them. The constants and their meanings are shown here: BLOCK_DECREMENT= The user clicked inside the scroll bar to decrease its value. BLOCK_INCREMENT= The user clicked inside the scroll bar to increase its value. TRACK =The slider was dragged. UNIT_DECREMENT =The button at the end of the scroll bar was clicked to decrease its value. UNIT_INCREMENT =The button at the end of the scroll bar was clicked to increase its value. AdjustmentEvent(Adjustable src, int id, int type, int data) • Here, src is a reference to the object that generated this event. The id specifies the event. The type of the adjustment is specified by type, and its associated data is data.
  • 12. The ComponentEvent Class • A ComponentEvent is generated when the size, position, or visibility of a component is changed. • There are four types of component events COMPONENT_HIDDEN = The component was hidden. COMPONENT_MOVED = The component was moved. COMPONENT_RESIZED = The component was resized. COMPONENT_SHOWN = The component became visible. ComponentEvent(Component src, int type) • src = is a reference to the object that generated this event. • type = The type of the event is specified.
  • 13. The ContainerEvent Class • AContainerEvent is generated when a component is added to or removed from a container. • There are two types of container events. COMPONENT_ADDED and COMPONENT_REMOVED ContainerEvent(Component src, int type, Component comp) • src = reference to the container that generated this event. • type = The type of the event is specified • comp = the component that has been added to or removed from the container.
  • 14. The FocusEvent Class • The FocusEvent Class A FocusEvent is generated when a component gains or loses input focus. • These events are identified by the integer constants FOCUS_GAINED and FOCUS_LOST. • FocusEvent is a subclass of ComponentEvent and has these constructors: FocusEvent(Component src, int type) FocusEvent(Component src, int type, boolean temporaryFlag) FocusEvent(Component src, int type, boolean temporaryFlag, Component other) • src = reference to the component that generated this event. • type = The type of the event is specified by. • temporaryFlag = temporaryFlag is set to true if the focus event is temporary. Otherwise, it is set to false. • The other component involved in the focus change, called the opposite component, is passed in other. Therefore, if a FOCUS_GAINED event occurred, other will refer to the component that lost focus. Conversely, if a FOCUS_LOST event occurred, other will refer to the component that gains focus.
  • 15. The InputEvent Class • The InputEvent Class is the abstract class. InputEvent is a subclass of ComponentEvent • InputEvent defines several integer constants that represent any modifiers, such as the control key being pressed, that might be associated with the event. • Its subclasses are KeyEvent and MouseEvent. • To test if a modifier was pressed at the time an event is generated boolean isControlDown( ) boolean isMetaDown( ) boolean isAltDown( ) boolean isAltGraphDown( ) boolean isShiftDown( )
  • 16. The ItemEvent Class • An ItemEvent is generated when a check box or a list item is clicked or when a checkable menu item is selected or deselected. • There are two types of item events, which are identified by the following integer constants: DESELECTED The user deselected an item. SELECTED The user selected an item. ItemEvent(ItemSelectable src, int type, Object entry, int state) • src = reference to the component that generated this event. • type = The type of the event is specified by. • entry = The specific item that generated the item event is passed . • State = The current state of that item.
  • 17. The KeyEvent Class • A KeyEvent is generated when keyboard input occurs. There are three types of key events, which are identified by these integer constants: KEY_PRESSED, KEY_RELEASED, and KEY_TYPED. • There are many other integer constants that are defined by KeyEvent. For example, VK_0 through VK_9 and VK_A through VK_Z define the ASCII equivalents of the numbers and letters. KeyEvent(Component src, int type, long when, int modifiers, int code, char ch) • src = reference to the component that generated the event. • type = type of the event is specified by • when = The system time at which the key was pressed is passed . • modifiers = indicates which modifiers were pressed when this key event occurred. • code = virtual key code, such as VK_UP, VK_A, and so forth, is passed in. • ch = The character equivalent (if one exists) is passed in. If no valid character exists, then ch contains CHAR_UNDEFINED.
  • 18. The MouseEvent Class • There are eight types of mouse events. MOUSE_CLICKED The user clicked the mouse. MOUSE_DRAGGED The user dragged the mouse. MOUSE_ENTERED The mouse entered a component. MOUSE_EXITED The mouse exited from a component. MOUSE_MOVED The mouse moved. MOUSE_PRESSED The mouse was pressed. MOUSE_RELEASED The mouse was released. MOUSE_WHEEL The mouse wheel was moved. MouseEvent(Component src, int type, long when, int modifiers, int x, int y, int clicks, boolean triggersPopup)
  • 19. The MouseWheelEvent Class • The MouseWheelEvent class encapsulates a mouse wheel event. It is a subclass of MouseEvent. • Mouse wheels are used for scrolling. MouseWheelEvent defines these two integer constants: WHEEL_BLOCK_SCROLL WHEEL_UNIT_SCROLL = A page-up or page-down scroll event occurred. =A line-up or line-down scroll event occurred. MouseWheelEvent(Component src, int type, long when, int modifiers, int x, int y, int clicks, boolean triggersPopup, int scrollHow, int amount, int count)
  • 20. The TextEvent Class • Instances of this class describe text events. These are generated by text fields and text areas when characters are entered by a user or Programram. TextEvent(Object src, int type) The WindowEvent Class WINDOW_ACTIVATED WINDOW_CLOSING WINDOW_DEICONIFIED WINDOW_ICONIFIED WINDOW_OPENED WINDOW_CLOSED WINDOW_DEACTIVATED WINDOW_GAINED_FOCUS WINDOW_LOST_FOCUS WINDOW_STATE_CHANGED
  • 21. The WindowEvent Class cont… • The next three constructors offer more detailed control: WindowEvent(Window src, int type, Window other) WindowEvent(Window src, int type, int fromState, int toState) WindowEvent(Window src, int type, Window other, int fromState, int toState) Sources of Events Button Choice Menu Text Check box List Scroll bar Window
  • 22. Event Listener Interfaces • the delegation event model has two parts: sources and listeners. • Listeners are created by implementing one or more of the interfaces defined by the java.awt.event package.
  • 23. The AdjustmentListener Interface • This interface defines the adjustmentValueChanged( ) method that is invoked when an adjustment event occurs. • Its general form is shown here: void adjustmentValueChanged(AdjustmentEvent ae) The ComponentListener Interface • This interface defines four methods that are invoked when a component is resized, moved, shown, or hidden. • Their general forms are shown here: void componentResized(ComponentEvent ce) void componentMoved(ComponentEvent ce) void componentShown(ComponentEvent ce) void componentHidden(ComponentEvent ce)
  • 24. The ContainerListener Interface • This interface contains two methods. When a component is added to a container, componentAdded( ) is invoked. When a component is removed from a container, componentRemoved( ) is invoked. • Their general forms are shown here: void componentAdded(ContainerEvent ce) void componentRemoved(ContainerEvent ce) • The FocusListener Interface • This interface defines two methods. When a component obtains keyboard focus, focusGained( ) is invoked. When a component loses keyboard focus, focusLost( ) is called. • Their general forms are shown here: void focusGained(FocusEvent fe) void focusLost(FocusEvent fe)
  • 25. The ItemListener Interface • This interface defines the itemStateChanged( ) method that is invoked when the state of an item changes. • Its general form is shown here: void itemStateChanged(ItemEvent ie) The KeyListener Interface • The general forms of these methods are shown here: void keyPressed(KeyEvent ke) void keyReleased(KeyEvent ke) void keyTyped(KeyEvent ke)
  • 26. The MouseListener Interface • This interface defines five methods. • The general forms of these methods are shown here: void mouseClicked(MouseEvent me) void mouseEntered(MouseEvent me) void mouseExited(MouseEvent me) void mousePressed(MouseEvent me) void mouseReleased(MouseEvent me) The MouseMotionListener Interface • This interface defines two methods. • Their general forms are shown here: void mouseDragged(MouseEvent me) void mouseMoved(MouseEvent me)
  • 27. The MouseWheelListener Interface • This interface defines the mouseWheelMoved( ) method that is invoked when the mouse wheel is moved. • Its general form is shown here: void mouseWheelMoved(MouseWheelEvent mwe) The TextListener Interface • This interface defines the textChanged( ) method that is invoked when a change occurs in a text area or text field. • Its general form is shown here: void textChanged(TextEvent te) • The WindowFocusListener Interface • This interface defines two methods: windowGainedFocus( ) and windowLostFocus( ). These are called when a window gains or loses input focus. • Their general forms are shown here: void windowGainedFocus(WindowEvent we) void windowLostFocus(WindowEvent we)
  • 28. The WindowListener Interface • This interface defines seven methods. • The general forms of these methods are void windowActivated(WindowEvent we) void windowClosed(WindowEvent we) void windowClosing(WindowEvent we) void windowDeactivated(WindowEvent we) void windowDeiconified(WindowEvent we) void windowIconified(WindowEvent we) void windowOpened(WindowEvent we)
  • 29. Using the Delegation Event Model • Using the delegation event model is actually quite easy. Just follow these two steps: 1. Implement the appropriate interface in the listener so that it will receive the type of event desired. 2.Implement code to register and unregister (if necessary) the listener as a recipient for the event notifications. Handling Mouse Events • To handle mouse events, you must implement the MouseListener and the MouseMotionListener interfaces. Program on page no 654