Java Reference
In-Depth Information
EXERCISES
You can download the source code for the examples in the topic and the solutions to the following exer-
1.
Modify the
Lottery
applet to present the six numbers selected in ascending sequence.
2.
Replace the action listener for the selection buttons in the
Lottery
applet with a mouse listener and
use the
mousePressed()
method to update the selection with a new value.
3.
Modify the
Lottery
applet to implement the mouse listener for a selection button as an inner class
to the
Lottery
class.
4.
Modify the
Lottery
applet to implement the control buttons on a toolbar based on
Action
objects.
5.
Change the
Lottery
applet to handle the
MOUSE_ENTERED
and
MOUSE_EXITED
events within the
toolbar buttons you added in the previous exercise and display a hand cursor.
6.
Add tooltips to the lucky number buttons and the toolbar buttons in the
Lottery
applet. (You can
make the tooltip the same for each of the lucky number buttons.)
• WHAT YOU LEARNED IN THIS CHAPTER
TOPIC
CONCEPT
Events
A user interaction generates an event in the context of a component.
Event Cat-
egories
Two categories of events are associated with a component: low-level events from the mouse or keyboard,
or window system events such as opening or closing a window; and semantic events that represent com-
ponent actions such as pressing a button or selecting a menu item.
Event Oc-
currences
Both low-level and semantic events can arise simultaneously.
Handling
Events
An event for a component can be handled by the component object itself, or by a separate object that im-
plements a listener interface corresponding to the event type.
Component
Event
Handling
A component that is to handle its own events does so by calling its
enableEvents()
method and imple-
menting the class method to process the kind of event that has been enabled.
Registered
Listeners
A listener object that is registered with a component receives notification of the events originating with
the component that correspond to the type(s) of events the listener can handle.
Low-level
Event
Listeners
A listener for low-level events requires several event-handling methods to be implemented.
Semantic
Event
Listeners
A listener for semantic events only requires a single event-handling method to be implemented.
Adapter
Classes
An adapter class defines a set of empty methods for one or more low-level event interfaces. You can cre-
ate your own class defining a low-level event listener by deriving your class from an adapter class and
then implementing the event-handling methods in which you are interested.