jdk/src/share/classes/java/awt/Choice.java

Print this page




  68  * the <code>Choice</code>.
  69  * <p>
  70  * @author      Sami Shaio
  71  * @author      Arthur van Hoff
  72  * @since       JDK1.0
  73  */
  74 /* No native methods here, but the constants are needed in the supporting JNI code */
  75 @GenerateNativeHeader
  76 public class Choice extends Component implements ItemSelectable, Accessible {
  77     /**
  78      * The items for the <code>Choice</code>.
  79      * This can be a <code>null</code> value.
  80      * @serial
  81      * @see #add(String)
  82      * @see #addItem(String)
  83      * @see #getItem(int)
  84      * @see #getItemCount()
  85      * @see #insert(String, int)
  86      * @see #remove(String)
  87      */
  88     Vector pItems;
  89 
  90     /**
  91      * The index of the current choice for this <code>Choice</code>
  92      * or -1 if nothing is selected.
  93      * @serial
  94      * @see #getSelectedItem()
  95      * @see #select(int)
  96      */
  97     int selectedIndex = -1;
  98 
  99     transient ItemListener itemListener;
 100 
 101     private static final String base = "choice";
 102     private static int nameCounter = 0;
 103 
 104     /*
 105      * JDK 1.1 serialVersionUID
 106      */
 107     private static final long serialVersionUID = -4075310674757313071L;
 108 


 112         /* initialize JNI field and method ids */
 113         if (!GraphicsEnvironment.isHeadless()) {
 114             initIDs();
 115         }
 116     }
 117 
 118     /**
 119      * Creates a new choice menu. The menu initially has no items in it.
 120      * <p>
 121      * By default, the first item added to the choice menu becomes the
 122      * selected item, until a different selection is made by the user
 123      * by calling one of the <code>select</code> methods.
 124      * @exception HeadlessException if GraphicsEnvironment.isHeadless()
 125      * returns true
 126      * @see       java.awt.GraphicsEnvironment#isHeadless
 127      * @see       #select(int)
 128      * @see       #select(java.lang.String)
 129      */
 130     public Choice() throws HeadlessException {
 131         GraphicsEnvironment.checkHeadless();
 132         pItems = new Vector();
 133     }
 134 
 135     /**
 136      * Constructs a name for this component.  Called by
 137      * <code>getName</code> when the name is <code>null</code>.
 138      */
 139     String constructComponentName() {
 140         synchronized (Choice.class) {
 141             return base + nameCounter++;
 142         }
 143     }
 144 
 145     /**
 146      * Creates the <code>Choice</code>'s peer.  This peer allows us
 147      * to change the look
 148      * of the <code>Choice</code> without changing its functionality.
 149      * @see     java.awt.Toolkit#createChoice(java.awt.Choice)
 150      * @see     java.awt.Component#getToolkit()
 151      */
 152     public void addNotify() {


 174     @Deprecated
 175     public int countItems() {
 176         return pItems.size();
 177     }
 178 
 179     /**
 180      * Gets the string at the specified index in this
 181      * <code>Choice</code> menu.
 182      * @param      index the index at which to begin
 183      * @see        #getItemCount
 184      */
 185     public String getItem(int index) {
 186         return getItemImpl(index);
 187     }
 188 
 189     /*
 190      * This is called by the native code, so client code can't
 191      * be called on the toolkit thread.
 192      */
 193     final String getItemImpl(int index) {
 194         return (String)pItems.elementAt(index);
 195     }
 196 
 197     /**
 198      * Adds an item to this <code>Choice</code> menu.
 199      * @param      item    the item to be added
 200      * @exception  NullPointerException   if the item's value is
 201      *                  <code>null</code>
 202      * @since      JDK1.1
 203      */
 204     public void add(String item) {
 205         addItem(item);
 206     }
 207 
 208     /**
 209      * Obsolete as of Java 2 platform v1.1.  Please use the
 210      * <code>add</code> method instead.
 211      * <p>
 212      * Adds an item to this <code>Choice</code> menu.
 213      * @param item the item to be added
 214      * @exception NullPointerException if the item's value is equal to


 507             return;
 508         }
 509         itemListener = AWTEventMulticaster.remove(itemListener, l);
 510     }
 511 
 512     /**
 513      * Returns an array of all the item listeners
 514      * registered on this choice.
 515      *
 516      * @return all of this choice's <code>ItemListener</code>s
 517      *         or an empty array if no item
 518      *         listeners are currently registered
 519      *
 520      * @see           #addItemListener
 521      * @see           #removeItemListener
 522      * @see           java.awt.event.ItemEvent
 523      * @see           java.awt.event.ItemListener
 524      * @since 1.4
 525      */
 526     public synchronized ItemListener[] getItemListeners() {
 527         return (ItemListener[])(getListeners(ItemListener.class));
 528     }
 529 
 530     /**
 531      * Returns an array of all the objects currently registered
 532      * as <code><em>Foo</em>Listener</code>s
 533      * upon this <code>Choice</code>.
 534      * <code><em>Foo</em>Listener</code>s are registered using the
 535      * <code>add<em>Foo</em>Listener</code> method.
 536      *
 537      * <p>
 538      * You can specify the <code>listenerType</code> argument
 539      * with a class literal, such as
 540      * <code><em>Foo</em>Listener.class</code>.
 541      * For example, you can query a
 542      * <code>Choice</code> <code>c</code>
 543      * for its item listeners with the following code:
 544      *
 545      * <pre>ItemListener[] ils = (ItemListener[])(c.getListeners(ItemListener.class));</pre>
 546      *
 547      * If no such listeners exist, this method returns an empty array.




  68  * the <code>Choice</code>.
  69  * <p>
  70  * @author      Sami Shaio
  71  * @author      Arthur van Hoff
  72  * @since       JDK1.0
  73  */
  74 /* No native methods here, but the constants are needed in the supporting JNI code */
  75 @GenerateNativeHeader
  76 public class Choice extends Component implements ItemSelectable, Accessible {
  77     /**
  78      * The items for the <code>Choice</code>.
  79      * This can be a <code>null</code> value.
  80      * @serial
  81      * @see #add(String)
  82      * @see #addItem(String)
  83      * @see #getItem(int)
  84      * @see #getItemCount()
  85      * @see #insert(String, int)
  86      * @see #remove(String)
  87      */
  88     Vector<String> pItems;
  89 
  90     /**
  91      * The index of the current choice for this <code>Choice</code>
  92      * or -1 if nothing is selected.
  93      * @serial
  94      * @see #getSelectedItem()
  95      * @see #select(int)
  96      */
  97     int selectedIndex = -1;
  98 
  99     transient ItemListener itemListener;
 100 
 101     private static final String base = "choice";
 102     private static int nameCounter = 0;
 103 
 104     /*
 105      * JDK 1.1 serialVersionUID
 106      */
 107     private static final long serialVersionUID = -4075310674757313071L;
 108 


 112         /* initialize JNI field and method ids */
 113         if (!GraphicsEnvironment.isHeadless()) {
 114             initIDs();
 115         }
 116     }
 117 
 118     /**
 119      * Creates a new choice menu. The menu initially has no items in it.
 120      * <p>
 121      * By default, the first item added to the choice menu becomes the
 122      * selected item, until a different selection is made by the user
 123      * by calling one of the <code>select</code> methods.
 124      * @exception HeadlessException if GraphicsEnvironment.isHeadless()
 125      * returns true
 126      * @see       java.awt.GraphicsEnvironment#isHeadless
 127      * @see       #select(int)
 128      * @see       #select(java.lang.String)
 129      */
 130     public Choice() throws HeadlessException {
 131         GraphicsEnvironment.checkHeadless();
 132         pItems = new Vector<>();
 133     }
 134 
 135     /**
 136      * Constructs a name for this component.  Called by
 137      * <code>getName</code> when the name is <code>null</code>.
 138      */
 139     String constructComponentName() {
 140         synchronized (Choice.class) {
 141             return base + nameCounter++;
 142         }
 143     }
 144 
 145     /**
 146      * Creates the <code>Choice</code>'s peer.  This peer allows us
 147      * to change the look
 148      * of the <code>Choice</code> without changing its functionality.
 149      * @see     java.awt.Toolkit#createChoice(java.awt.Choice)
 150      * @see     java.awt.Component#getToolkit()
 151      */
 152     public void addNotify() {


 174     @Deprecated
 175     public int countItems() {
 176         return pItems.size();
 177     }
 178 
 179     /**
 180      * Gets the string at the specified index in this
 181      * <code>Choice</code> menu.
 182      * @param      index the index at which to begin
 183      * @see        #getItemCount
 184      */
 185     public String getItem(int index) {
 186         return getItemImpl(index);
 187     }
 188 
 189     /*
 190      * This is called by the native code, so client code can't
 191      * be called on the toolkit thread.
 192      */
 193     final String getItemImpl(int index) {
 194         return pItems.elementAt(index);
 195     }
 196 
 197     /**
 198      * Adds an item to this <code>Choice</code> menu.
 199      * @param      item    the item to be added
 200      * @exception  NullPointerException   if the item's value is
 201      *                  <code>null</code>
 202      * @since      JDK1.1
 203      */
 204     public void add(String item) {
 205         addItem(item);
 206     }
 207 
 208     /**
 209      * Obsolete as of Java 2 platform v1.1.  Please use the
 210      * <code>add</code> method instead.
 211      * <p>
 212      * Adds an item to this <code>Choice</code> menu.
 213      * @param item the item to be added
 214      * @exception NullPointerException if the item's value is equal to


 507             return;
 508         }
 509         itemListener = AWTEventMulticaster.remove(itemListener, l);
 510     }
 511 
 512     /**
 513      * Returns an array of all the item listeners
 514      * registered on this choice.
 515      *
 516      * @return all of this choice's <code>ItemListener</code>s
 517      *         or an empty array if no item
 518      *         listeners are currently registered
 519      *
 520      * @see           #addItemListener
 521      * @see           #removeItemListener
 522      * @see           java.awt.event.ItemEvent
 523      * @see           java.awt.event.ItemListener
 524      * @since 1.4
 525      */
 526     public synchronized ItemListener[] getItemListeners() {
 527         return getListeners(ItemListener.class);
 528     }
 529 
 530     /**
 531      * Returns an array of all the objects currently registered
 532      * as <code><em>Foo</em>Listener</code>s
 533      * upon this <code>Choice</code>.
 534      * <code><em>Foo</em>Listener</code>s are registered using the
 535      * <code>add<em>Foo</em>Listener</code> method.
 536      *
 537      * <p>
 538      * You can specify the <code>listenerType</code> argument
 539      * with a class literal, such as
 540      * <code><em>Foo</em>Listener.class</code>.
 541      * For example, you can query a
 542      * <code>Choice</code> <code>c</code>
 543      * for its item listeners with the following code:
 544      *
 545      * <pre>ItemListener[] ils = (ItemListener[])(c.getListeners(ItemListener.class));</pre>
 546      *
 547      * If no such listeners exist, this method returns an empty array.