Java Reference
In-Depth Information
You have a listener interface defined for each of the three semantic event types that I have introduced so far,
and they each declare a single method:
ActionListener defines actionPerformed(ActionEvent e) .
ItemListener defines itemStateChanged(ItemEvent e) .
AdjustmentListener defines adjustmentValueChanged(AdjustmentEvent e) .
All three methods have a void return type. Because each of these semantic event listener interfaces de-
clares only one method, there's no need for corresponding adapter classes. The adapter classes for the low-
level events were there only because of the number of methods involved in each listener interface. To define
your semantic event listener objects, you just define a class that implements the appropriate listener inter-
face. You can try that out by implementing a simple applet now, and then see how you can deal with semant-
ic events in a more complicated context by adding to the Sketcher program later.
SEMANTIC EVENT HANDLING IN APPLETS
Event handling in an applet is exactly the same as in an application, but you ought to see it for yourself. Let's
see how you might handle events for buttons in an applet. You can create an applet that uses some buttons
that have listeners. To make this example a bit more gripping, I'm throwing in the possibility of monetary
gain. That's interesting to almost everybody.
Let's suppose you want to implement an applet that creates a set of random numbers for a lottery entry.
The requirement is to generate six different random numbers between 1 and 49. It would also be nice to
be able to change a single number if you don't like it, so you add that capability as well. Because the local
lottery may not be like this, you implement the applet to make it easily adaptable to fit local requirements.
By displaying the six selected numbers on buttons, you can provide for changing one of the choices by
processing the action event for that button. Thus, clicking a button provides another number. You also add
a couple of control buttons, one to make a new selection for a complete set of lottery numbers, and another
just for fun to change the button color. Figure 18-5 shows how the applet looks when running under ap-
pletviewer .
FIGURE 18-5
 
 
Search WWH ::




Custom Search