Professional Documents
Culture Documents
Outlines
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.
For example, a button is the source object for a button-clicking action
event.
Introduction
An event is an instance of an event class.
The root class of the event classes is java.util.EventObject.
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.
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.
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.
Listener Interfaces
Listener Methods
ActionListener
actionPerformed(ActionEvent)
ItemListener
itemStateChanged(ItemEvent)
MouseListener
mouseClicked(MouseEvent)
mouseEntered(MouseEvent)
mouseExited(MouseEvent)
mousePressed(MouseEvent)
mouseReleased(MouseEvent)
MouseMotionListener
KeyListener
mouseDragged(MouseEvent)
mouseMoved(MouseEvent)
keyPressed(KeyEvent)
keyReleased(KeyEvent)
keyTyped(KeyEvent)
ActionListener
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.
ActionEvent Class
Method
Purpose
String getActionCommand()
int getModifiers()
Object getSource()
ItemListener 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.
ItemEvent class
Method
Purpose
Object getItem()
ItemSelectable getItemSelectable()
int getStateChange()
KeyListener 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
Purpose
keyTyped(KeyEvent)
keyPressed(KeyEvent)
keyReleased(KeyEvent)
KeyEvent class
Method
int getKeyChar()
Purpose
Obtains the Unicode character associated
with this event.
int getKeyCode()
boolean isActionKey()
MouseListener 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.
Purpose
mouseClicked(MouseEvent)
mouseEntered(MouseEvent)
mouseExited(MouseEvent)
mousePressed(MouseEvent)
mouseReleased(MouseEvent)
MouseEvent class
Method
Purpose
int getClickCount()
int getButton()
int getX()
int getY()
MouseAdapter Class
MouseAdapter class provides an empty implementation of all the
methods in MouseListener interface. This class exists as convenience
for creating listener objects.
Extend this class to create a MouseEvent listener and override the
methods for the events of interest.
Create a listener object using the extended class and then register it
with a component using the component's addMouseListener method.
When a mouse button is pressed, released, or clicked (pressed and
released), or when the mouse cursor enters or exits the component,
the relevant method in the listener object is invoked and the
MouseEvent is passed to it.
MouseMotionListener Interface
Mouse-motion events notify when the user uses the mouse (or a
similar input device) to move the onscreen cursor.
If an application requires the detection of both mouse events and
mouse-motion events, use the MouseInputAdapter class.
It implements the MouseInputListener a convenient interface that
implements both the MouseListener and MouseMotionListener
interfaces.
mouseDragged(MouseEvent)
mouseMoved(MouseEvent)
Purpose
Called in response to the user moving
the mouse while holding a mouse
button down. This event is fired by the
component that fired the most recent
mouse-pressed event, even if the
cursor is no longer over that
component.
Called in response to the user moving
the mouse with no mouse buttons
pressed. This event is fired by the
component that's currently under the
cursor.