Java The Complete Reference, Seventh Edition

(Greg DeLong) #1

Chapter 24: Using AWT Controls, Layout Managers, and Menus 753


List. It callsenableEvents( )so that both action and item events may be received by this
object. When an entry is selected or deselected,processItemEvent( )is called. When an
entry is double-clicked,processActionEvent( )is also called. Both methods display a string
and then hand control to the superclass.


/*








  • */
    import java.awt.*;
    import java.awt.event.*;
    import java.applet.*;


public class ListDemo2 extends Applet {
MyList list;
public void init() {
list = new MyList();
list.add("Red");
list.add("Green");
list.add("Blue");
add(list);
}
class MyList extends List {
public MyList() {
enableEvents(AWTEvent.ITEM_EVENT_MASK |
AWTEvent.ACTION_EVENT_MASK);
}
protected void processActionEvent(ActionEvent ae) {
showStatus("Action event: " + ae.getActionCommand());
super.processActionEvent(ae);
}
protected void processItemEvent(ItemEvent ie) {
showStatus("Item event: " + getSelectedItem());
super.processItemEvent(ie);
}
}
}


Extending Scrollbar

The following program creates an applet that displays a scroll bar. When this control is
manipulated, a string is displayed on the status line of the applet viewer or browser. That
string includes the value represented by the scroll bar.
There is one top-level class namedScrollbarDemo2that extendsApplet. Itsinit( )
method creates a scroll bar element and adds it to the applet.MyScrollbaris an inner class
that extendsScrollbar. It callsenableEvents( )so that adjustment events may be received
by this object. When the scroll bar is manipulated,processAdjustmentEvent( )is called.
When an entry is selected,processAdjustmentEvent( )is called. It displays a string and
then hands control to the superclass.


/*









Free download pdf