OOP with JAVA
CS122
Noushin Saba
Lecture # 18
GUI and Event Driven Programming -II
Event Handling
• Event handling in Java is the procedure that controls an event and performs
appropriate action if it occurs
• The code or set of instructions used to implement it is known as the Event
handler
o Event source → Object where the event occurs
o Event listener → Responsible for taking appropriate actions when an event occurs
• These Listeners must be registered with the source object for the listener to receive event
notifications
Events in Java
• Events in Java represent the change in the state of any object
• Events occur when the user interacts with the interface
o Clicking a button
o Moving the mouse
o Typing a character
o Selecting an item from a list
o Scrolling the page
• Event handling in Java is the process of controlling an event and taking
appropriate action if one occurs
Types of Events in Java
Foreground Events
• These events necessitate the user's direct participation
• They are produced as a result of a user interacting with graphical components in a Graphical
User Interface
• Examples of foreground events include button clicks, key presses, mouse movements, and
window resizing
Background Events
• Background events are those that do not require end-user interaction
• Operating system interrupts and hardware or software failures are examples of background
events
How are Events Handled?
Delegation Event Model
• Defines a standardized and compatible mechanism for generating and processing events
▪ Source generates an event and sends it to one or more listeners
▪ Listener sits and waits for an event to occur
• When it gets an event, it is processed by the listener and returned
• User interface elements can delegate the processing of an event to a different function
Components of Event Handling in Java
Event Handler
• An event handler is a function or method that executes program statements in response to an
event
• A software program that processes activities such as keystrokes and mouse movements is
what an event handler is
• Event handlers in Web sites make Web content dynamic.
Event Sources
• An object on which an event occurs is referred to as a source
• The source is in charge of informing the handler about the event that occurred
• There are various sources like buttons, checkboxes, lists, menu items, choices, scrollbars, text
components, windows, etc.
Components of Event Handling in Java
(cont.)
Event Listeners
• When an event occurs, an object named an event listener is called
• The listeners need two things: first, they must be registered with a source; however, they can
be registered with multiple sources to receive event notifications
• Second, it must put in place the methods for receiving and processing notifications
• Set of interfaces defines the methods for dealing with events
Event Classes and Listener Interfaces in Java
Event Classes Description Listener Interface
When a button is clicked or a list item is
ActionEvent doubleclicked, an ActionEvent is triggered. ActionListener
This event indicates a mouse action occurred in a
MouseEvent component MouseListener
The Key event is triggered when the character is
KeyEvent entered using the keyboard. KeyListener
An event that indicates whether an item was selected
ItemEvent or not. ItemListener
TextEvent when the value of a textarea or text field is changed TextListener
Event Classes and Listener Interfaces in Java
Event Classes Description Listener Interface
MouseWheelEvent generated when the mouse wheel is rotated MouseWheelListener
The object of this class represents the change in the
state of a window and are generated when the
WindowEvent WindowListener
window is activated, deactivated, deiconified,
iconified, opened or closed
when a component is hidden, moved, resized, or
ComponentEvent ComponentEventListener
made visible
when a component is added or removed from a
ContainerEvent ContainerListener
container
AdjustmentEvent when the scroll bar is manipulated AdjustmentListener
FocusEvent when a component gains or loses keyboard focus FocusListener
Steps to Perform Event Handling in Java
Implement appropriate interface in the class
• The first step is to implement an appropriate interface in the class
Register the component with the listener
• Once done with the implementation of the interface in the class, the second step is to register the
created components with listeners, which can be done using inbuilt functions