0% found this document useful (0 votes)
168 views21 pages

Event Handling and GUI Programming

The document discusses event handling and GUI programming in Java. It covers event handling mechanisms including the delegation event model, commonly used event classes like AWTEvent, and event listener interfaces. It provides details on how sources generate events and listeners receive and process events. It also gives an example of implementing mouse events in a simple mouse application to demonstrate event-driven programming.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
168 views21 pages

Event Handling and GUI Programming

The document discusses event handling and GUI programming in Java. It covers event handling mechanisms including the delegation event model, commonly used event classes like AWTEvent, and event listener interfaces. It provides details on how sources generate events and listeners receive and process events. It also gives an example of implementing mouse events in a simple mouse application to demonstrate event-driven programming.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 21

Event Handling and GUI

Programming
Event Handling mechanism, Event Delegation, Event and KeyEvent Classes, Event
Listener
Interfaces. GUI Programming with JavaFX: UI Controls, Layout Classes, Collection
Classes,
Media Classes.
EVENTS
• EVENTS FROM GRAPHICAL USER INTERFACE
• There are several types of events, including those generated by the mouse, the
keyboard, and various GUI controls, such as a push button, scroll bar, or check
box.
• OTHER EVENTS
• When a timer expires
• A counter exceeds a value,
• A software or hardware failure occurs, or an operation is completed.
• You are free to define events that are appropriate for your application.
Event Handling

• Essential for creating event driven programs


• Example an event driven program – GUI application
• Packages that support event driven programming
• java.util, java.awt, java.awt.event.
• Beginning with JDK 9, java.awt and java.awt.event are part of the java.desktop
module, and java.util is part of the java.base
Event Handling Mechanisms
• Two types of Mechanisms
• Java 1.0 model (deprecated – not to be used in programs)
• Components are organised as an hierarchy
• An event traverses the hierarchy till handled by a component
• Java 1.1 and later (Delegation event model)
• Comprises sources and listeners
• Listener has to be registered with a source
• The response to an event at the source is given by the listener
• APPLICATION LOGIC IS SEPARATED FROM USER INTERFACE LOGIC
Java - Delegation Event Model

SOURCES
Subclasses of
Component in AWT EVENTS
Subclasses of EventObject
LISTENERS
Implement interfaces to
generate respond to events
For AWTEvents listener
interfaces are in
java.awt.event package
Event User Interface Components that generates Events

sources

• Any source derived from Component such as Frame can generate


events
Event source class
• Have methods to register listeners to send notifications
• Each event type has a listener method which can add multiple
listeners

Multicast of notifications occur (Sent to many recepients)


Few event types can send notification to only one listener
where the method is of the following form The methods that add or remove listeners are
provided by the source that generates events. For
example, Component, which is a top-level class
defined by the AWT, provides methods to add
Unicast of notifications occur and remove keyboard and mouse event
listeners.
Sources can unregister a specific type of event using
Event classes
• Subclasses of EventObject (java.util package)
• Methods in EventObject
• EventObject(Object src) – generates event
• Object getSource() – return the source of the event
• int getID() – return the type of the event
• Class used for GUI – AWTEvent, Swing (Subclasses of EventObject)
Event classes contd…

• Commonly used subclasses of AWTEvent (present in java.awt.event)


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.
In other words, the listener must supply the event handlers.
• Interfaces to implement by a listener in a AWT GUI are in java.awt.event
• Example MouseMotionListener has methods to move and drag of mouse
• The method that implements the response should be quick
• Long time actions should be started as a separate thread
Event
Listener
Using the delegation event model

• Implement the appropriate interface in the listener so that it can


receive the type of event desired.
• Implement code to register and unregister (if necessary) the listener as
a recipient for the event notifications.
Using delegation event model
• A source may generate several types of events.
• Each event must be registered separately.
• Also, an object may register to receive several types of
events, but it must implement all of the interfaces that are
required to receive these events.
• In all cases, an event handler must return quickly. As
explained earlier, an event handler must not retain control for
an extended period of time.
Example – Mouse Application
Implementation for the Mouse Application
• Extend Frame class for the Window
• Register with the MouseListener and MouseMotionListener
• Implement methods in MouseListener and MouseMotionListener
interfaces
Class for the Frame component and
with interfaces for Key board events

Member variables to store


required details

Registering Listeners (mouse, mouse motion and


close button in the Window)
Implementation for Frame (the Window)

Listener for the close button in the Window


• MouseListener Interface

• MouseMotionListener Interface
Implementing MouseListener
Implementing MouseMotionListener
Initialising the Application

You might also like