Event Listener (cont.)

Download Report

Transcript Event Listener (cont.)

Interactive Programs
Java API
Terminology

Event—an action or occurrence, not part
of a program, detected by the program.
Events can be
user actions--clicking a mouse button,
pressing a key, hovering mouse over text box
 system occurrences—running out of memory,
raising exceptions


Event-driven program—responds to
events, such as most programs in
Windows environment
Terminology (cont.)


Event Listener— an interface (like a class,
but provides only methods)—that will
detect an event. The interface provides
only an outline of the methods, and the
programmer must write the code for each
method.
Event Handler— code that is executed
when an even is detected
To Make Interactive Program

Steps to make a program interactive:.
1. Identify components with anticipated
events (e.g., a button with a buttonclick)
2. Associate event listeners with the
components
3. Write event handlers
Types of Events



Action event
 occurs when a button, check box, radio
button, or text field is clicked.
Adjustment event
 occurs occur when a scrollbar is adjusted by
dragging an arrow on the bar or clicking
anywhere on the bar.
Focus event
 occurs when component gains or loses
focus on the GUI. E.g., a text field can gain
a focus by clicking the mouse anywhere
inside it or by pressing the tab key
Types of Events (cont.)

Key event

Mouse event
 occurs
when a particular key is pressed
on the keyboard. An event listener can
distinguish each key so that appropriate
event handlers can be provided.
 occurs
when the mouse is clicked over a
component, when it hovers over a
component, or when it leaves the
component's area.
Event Listener


Event listener—an interface corresponding
to each type of event.
When an event listener is associated with
a class, the class must implement all
methods in the event listener.
Event Listener (cont.)





Interface ActionListener contains method
actionPerformed(), to handle action events.
Interface AdjustmentListener contains method
adjustmentValueChanged() to handle the adjustment
events.
Interface FocusListener contains two methods:
focusGained() and focusLost() to handle focus events.
Interface KeyListener contains three methods:
keyPressed(), keyReleased(), and keyTyped(), to handle
key events.
Interface MouseListener contains five methods:
moustClicked(), mouseEntered(), mouseExited(),
moustPressed(), and mouseReleased() to handle the
mouse events.
Event Listener (cont.)
Name
ActionListener
AdjustmentListener
FocusListener
KeyListener
MouseListener
Method(s)
actionPerformed()
adjustmentValueChanged()
focusGained(); focusLost()
keyPressed(); keyReleased();
keyTyped()
mouseClicked();
mouseEntered();
mouseExited();
moustPressed();
mouseReleased()
Implementing Interface



To make use of the methods in an event listener
interface in a class, it is implemented , instead of
extended, by the class.
If there are more than one interface to be
brought in, they can be listed one after another
with commas separating them.
The following code segment involves interfaces
ActionListener and FocusListener in the
EventDemo class.
Implementing Interface (cont.)
public class EventDemo extends JFrame
implements
ActionListener, FocusListener {
... // code for EventDemo class
}
Example: EventDemo.java


The following is an interactive program that
displays a message in a text field, whose
background color can be changed by the user
clicking one of the three buttons.
In addition, the text style of the message is
changed to italic when the field gains focus and
returns to plain style when it loses focus. (The
screen shot shows the window after the "cyan"
button is clicked. Note that the text style is plain
because the text field has lost its focus to the
"cyan" button.)
Example: EventDemo.java (cont.)
Go to an Applet version of EventDemo
Example: EventDemo.java (cont.)

Note
In the class declaration, ActionListener and
FocusListener are implemented.
 Objects—buttons and textfield—are declared as
instance variables (global to the class).
 In constructor EventDemo(),
 buttons are associated with event listeners.
E.g.,
green.addActionListener(this);
 Here, this refers the current class which will do
the listening—like the “ear”

Example: EventDemo.java (cont.)
 Buttons
are added to a container
 Buttons must be associated with event listeners
before the buttons are added to a container.
 Method actionPerformed() is event handler.
 Kind of object receiving the event is checked.
 Action is performed depending on the kind of
object receiving the event.
 Method main() simply instantiates the EventDemo
class and makes the object visible.
Example: DistanceConverter

Program DistanceConverter attaches some
calculation steps to event handlers. Its interface
consists of two labels, two text fields, and four
buttons.
Go to an Applet
version of EventDemo
Example: DistanceConverter (cont.)


Text boxes
 For inputting distances
Buttons
 “To Meter”—when clicked, converts feet value to
meter value and displays in appropriate box
 "To Feet"—when clicked, converts meters to
feet value and displays in appropriate box.
 "Clear“—clears text fields
 "Instructions“—pops up a message box with a
short explanation of the program.
Example: DistanceConverter (cont.)


Here is the source code for DistanceConverter.java
Note that:
 In constructor DistanceConvert()
 super() is an invocation to a constructor of the
frame's super class.
 setdefaultCloseOperation() calls a method
inherited from JFrame
 Container pane uses a grid layout of 4 rows
by 2 columns
Example: DistanceConverter (cont.)

In event handler actionPerformed()
 With interface ActionListener, this is the only
method that needs to be implementd.
 After the statement
Object src = e.getSource()
depending on the identity of src, different
actions are performed.
 To check for valid input, note the use of
strFeet.equals(""), and not
strFeet == "",
because strFeet is a reference, or a pointer.
Example: DistanceConverter (cont.)

In the statement
txtMeter.setText(decForm.format(numMeter)+" ")
decForm is a formatting object which was
instantiated from the DecimalFormat class, at
the top of this method.
The pattern in its constructor-- "#.00"--specifies
two decimal points. Thus, expression
decForm.format(numMeter) will return a string
of digits with two decimal points.
Exercise


Recall a simple java program created in the last
chapter—GUI--named GUIDemo,java, which consisted
of labels for first & last names, textboxes for first & last
names, and buttons to start and quit the program.
Modify the program as follows:


Arrange the components using the grid layout manager. (3, 2)
Associate an event handler to the start button so that when it is
clicked, a message box (showMessageDialog()) outputs a
greeting. The greeting is of the form “Welcome to Hawaii,
Charles Chang”, where “Charles” and “Chang” are contents of
the two text boxes.
Exercise (cont.)

Associate an event with the quit button so that
when it is clicked, the program ends.
(System.exit(0))
Exercise (cont.)
GuiDemo.java code