home *** CD-ROM | disk | FTP | other *** search
/ PC Online 1998 January / PCO0198.ISO / 1&1 / java.z / java_301 / java / awt / Choice.class (.txt) < prev    next >
Encoding:
Java Class File  |  1996-10-20  |  2.4 KB  |  76 lines

  1. package java.awt;
  2.  
  3. import java.awt.peer.ChoicePeer;
  4. import java.util.Vector;
  5.  
  6. public class Choice extends Component {
  7.    Vector pItems = new Vector();
  8.    int selectedIndex = -1;
  9.  
  10.    public synchronized void addNotify() {
  11.       super.peer = ((Component)this).getToolkit().createChoice(this);
  12.       super.addNotify();
  13.    }
  14.  
  15.    public int countItems() {
  16.       return this.pItems.size();
  17.    }
  18.  
  19.    public String getItem(int index) {
  20.       return (String)this.pItems.elementAt(index);
  21.    }
  22.  
  23.    public synchronized void addItem(String item) {
  24.       if (item == null) {
  25.          throw new NullPointerException();
  26.       } else {
  27.          this.pItems.addElement(item);
  28.          ChoicePeer peer = (ChoicePeer)super.peer;
  29.          if (peer != null) {
  30.             peer.addItem(item, this.pItems.size() - 1);
  31.          }
  32.  
  33.          if (this.selectedIndex < 0) {
  34.             this.select(0);
  35.          }
  36.  
  37.       }
  38.    }
  39.  
  40.    public String getSelectedItem() {
  41.       int selectedIndex = this.selectedIndex;
  42.       return selectedIndex >= 0 ? this.getItem(selectedIndex) : null;
  43.    }
  44.  
  45.    public int getSelectedIndex() {
  46.       return this.selectedIndex;
  47.    }
  48.  
  49.    public synchronized void select(int pos) {
  50.       if (pos >= this.pItems.size()) {
  51.          throw new IllegalArgumentException("illegal Choice item position: " + pos);
  52.       } else {
  53.          if (this.pItems.size() > 0) {
  54.             this.selectedIndex = pos;
  55.             ChoicePeer peer = (ChoicePeer)super.peer;
  56.             if (peer != null) {
  57.                peer.select(pos);
  58.             }
  59.          }
  60.  
  61.       }
  62.    }
  63.  
  64.    public void select(String str) {
  65.       int index = this.pItems.indexOf(str);
  66.       if (index >= 0) {
  67.          this.select(index);
  68.       }
  69.  
  70.    }
  71.  
  72.    protected String paramString() {
  73.       return super.paramString() + ",current=" + this.getSelectedItem();
  74.    }
  75. }
  76.