event. - IHMC Public Cmaps (2)

Download Report

Transcript event. - IHMC Public Cmaps (2)

Event Handling
An action involving a GUI object, such
as clicking a button, is called an event.
The mechanism to process events is
called event handling.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display.
Some Java Swing GUI Objects
GUI objects are added to the content pane of the JFrame
window
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display.
Delegation-Based Event Model
The event-handling model of Java is
based on the concept known as the
delegation-based event model.
With this model, event handling is
implemented by two types of objects:
• event source objects (e.g., JButton)
• event listener objects.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display.
Event Sources & Event Listeners
An event source object is a GUI object
where an event occurs. An event
source generates events.
An event listener object is an object that
includes a method that gets executed
in response to the generated events.
When an event is generated, the
system notifies the relevant event
listener objects.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display.
Action Events
There are many different kinds of events,
but the most common one is an action
event (mouse events are covered in
chapter 14).
For the generated events to be processed,
we must associate, or register, event
listeners to the event sources.
If event sources have no registered
listeners, the generated events are
ignored.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display.
Handling Action Events
An object that can be registered as an
action listener must be an instance of
a class that is declared specifically for
the purpose. We call such classes
action listener classes.
To associate an action listener to an
action event source, we call the event
source’s addActionListener method
with the action listener as its
argument.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display.
Handling Action Events
// A JButton is an Action event source
private JButton okButton;
.
. (set up button and place on content pane)
.
// For JButtons that are added to the GUI
// we register an action listener method
// by calling the event source’s
// addActionListener method
ButtonHandler handler=new ButtonHandler();
okButton.addActionListener(handler);
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display.
Handling Action Events
A single listener can be associated to
multiple event sources.
Likewise, multiple listeners can be
associated to a single event source.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display.
Handling Action Events
When an event source generates an
event, the system checks for matching
registered listeners.
If there is no matching listener, the
event is ignored.
If there is a matching listener, the
system notifies the listener by calling
the listener’s corresponding method.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display.
Handling Action Events
In the case of action events, the method
called is actionPerformed.
To ensure the programmer includes the
necessary actionPerformed method in
the action listener class, the class must
be defined in a specific way.
import java.awt.event.*;
class ButtonHandler implements
ActionListener {
...
}
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display.
Handling Action Events
ActionListener is an interface (referred
to as a Java interface), not a class.
Like a class, an interface is a reference
data type, but unlike a class, an
interface includes only constants and
abstract methods.
An abstract method has only the
method header, or prototype; it has no
method body.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display.
Handling Action Events
interface ActionListner {
public void actionPerformed(ActionEvent
event);
}
A class that implements a Java interface
must provide the method body to all the
abstract methods defined in the interface.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display.
Handling Action Events
By requiring an object we pass as an
argument to the addActionListener
method to be an instance of a class
that implements the ActionListener
interface, the system ensures that this
object will include the necessary
actionPerformed method.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display.
ActionEvent Argument
public void actionPerformed(ActionEvent evt);
ActionEvent class includes methods to
access the properties of a generated
event.
Approaches to getting information from an
event:
String buttonText = evt.getActionCommand();
… or …
JButton clickedButton=(JButton)evt.getSource();
String buttonText = clickedButton.getText();
… or …
Object eventObject = evt.getSource();
if (eventObject.equals(okButton)){ … }
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display.
Handling Action Events Summary
To handle events from action event
sources in a GUI interface (e.g., a
JButton).
1) The event listener object must
implement the ActionListener interface
import java.awt.event.*;
class ButtonHandler implements
ActionListener {
...
}
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display.
Handling Action Events Summary
2) The event listener object must be
registered with the event action source.
private JButton okButton;
.
.
.
// register event listener object
ButtonHandler handler=new ButtonHandler();
okButton.addActionListener(handler);
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display.
Handling Action Events Summary
3) The event listener object must implement an
actionPerformed method that the system will
invoke when the event occurs (i.e., the button
is clicked).
import java.awt.event.*;
class ButtonHandler implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
.
. (code to handle button here)
.
}
}
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display.
Handling Action Events Summary
To simplify things, we can place the
event sources and event listeners for
those event sources in the same class.
See Java code examples for this
chapter.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display.