List. It calls enableEvents( ) 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.
/*
* <applet code=ListDemo2 width=300 height=100>
* </applet>
*/
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 named ScrollbarDemo2 that extends Applet. Its init( )
method creates a scroll bar element and adds it to the applet. MyScrollbar is an inner class
that extends Scrollbar. It calls enableEvents( ) 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.
/*
* <applet code=ScrollbarDemo2 width=300 height=100>
* </applet>
Search WWH :
Custom Search
Previous Page
Java SE 6 Topic Index
Next Page
Java SE 6 Bookmarks
Home