Beruflich Dokumente
Kultur Dokumente
Pre-Assessment Questions
1. Identify the package that defines the JApplet class in Java.
a. javax.swing
b. java.awt
c. java.io
d. java.net
Solutions to Pre-assessment
Questions
1. a. javax.swing
2. c. stop()
3. b. Choice
4. a. JPanel
5. d. GridBagLayout Manager
Objectives
In this lesson, you will learn about:
Event-handling in Java
Event Classes
EventListener Interfaces
Adapter Classes
Event-Handling in Java
An object that describes a change of state in a source component is called an
event.
The source components can be the elements of the Graphical User Interface
(GUI).
Events are supported by the classes and interfaces defined in the
java.awt.event package.
Identifying the Source Of Events:
An event source is an object that generates an event.
An event source registers some listeners, which receive notifications about
a specific type of event generated by that particular event source.
All the registered listeners are notified about the generation of an event
and receive a copy of the event object.
This is known as multicasting the event.
Some sources allow only single listener to register.
This is known as unicasting of event.
NIIT Java Programming Lesson 2B / Slide 6 of 27
Event-handling
Event Classes
The Java event class hierarchy:
Constants Description
Constants Description
Methods Description
Methods Description
Interface Description
Demonstration-Using the
ActionListener Interface
Problem Statement
Create an applet that contains a button. When you click the button,
the label of the button is changed from "Click here" to "Button
clicked".
Solution
To solve the above problem, perform the following tasks:
Code the application
Compile and execute the application
Adapter Classes
The Java programming language provides adapter classes, which implement
the event listener interfaces containing more than one event-handling
method.
An adapter class provides an empty implementation of the event-handling
methods in an event listener interface.
The adapter classes are useful because they allows you to receive and
process only some of the events that are handled by a particular event
listener interface.
You can define a class that acts as an event listener by extending one of the
adapter classes and overriding its methods to handle a particular type of
event.
Summary
In this lesson, you learned:
The components of an event-driven program are:
Event Source: Refers to AWT components such as Button, List,
Checkbox, and Scrollbar that generate events.
Event Listener- Refers to any object that receives messages or
events.
Event Handler- Refers to the method that receives and processes the
event. The event handler method takes an Event object as a
parameter.
The various event classes in Java, such as the ActionEvent and MouseEvent
classes.
To handle action events, you need to register the listener object that
implements the ActionListener interface.
To handle mouse events, you need to register the listener object that
implements the MouseListener interface.
Events generated by mouse are of two types, mouse events and mouse
motion events.
NIIT Java Programming Lesson 2B / Slide 26 of 27
Event-handling
Summary (Contd.)
Mouse events include, pressing a mouse button, releasing a mouse
button, clicking a mouse button, entering of a mouse in a component
area, exiting of a mouse from a component area.
Mouse motion events include, moving a mouse and dragging a mouse.
Adapter classes provides an empty implementation of all the methods
in an event listener interface.