Packages  This Package  Prev  Next  Index  

§1.8 Class Choice

public  class  java.awt.Choice
    extends  java.awt.Component  (II-§1.10)
{
        // Constructors
    public Choice();	§1.8.1

        // Methods
    public void addItem(String  item);	§1.8.2
    public void addNotify();	§1.8.3
    public int countItems();	§1.8.4
    public String getItem(int  index);	§1.8.5
    public int getSelectedIndex();	§1.8.6
    public String getSelectedItem();	§1.8.7
    protected String paramString();	§1.8.8
    public void select(int  pos);	§1.8.9
    public void select(String  str);	§1.8.10
}
The Choice class presents a pop-up menu of choices. The current choice is displayed as the title of the menu.

For example, the code:


Choice ColorChooser = new Choice(); ColorChooser.addItem("Green"); ColorChooser.addItem("Red"); ColorChooser.addItem("Blue");
produces the following pop-up menu, after it has been added to a panel:.

In the picture, "Green" is the current choice. Pushing the mouse button down on on causes a menu to appear with the current choice highlighted.

After any choice is made,1 AWT sends an action event (II-§1.14.11) to the choice menu. The event's target is the choice menu, and its object is the string label of the currently selected item. An application should override the action method (II-§1.10.1) of the choice menu or of one of its parent containers in order to cause some action to occur.


Constructors

Choice

public Choice()
Creates a new choice menu. The menu initially has no items in it.
By default, the first item added to the choice menu becomes the selected item, until a different selection is made by the user or by calling one of the select methods (§1.8.9, §1.8.10).


Methods

addItem

public void addItem(String item)
Adds an item to this choice menu.
Parameters:
item - the item to be added
Throws
NullPointerException (I-§1.40)
If the item's value is equal to null.

addNotify

public void addNotify()
This method calls the createChoice method (II-§1.41.7) of this object's toolkit (II-§1.10.20) in order to create a ChoicePeer (II-§3.5) for this button. This peer allows the application to change the look of a choice mneu without changing its functionality.


Most applications do not call this method directly.
Overrides:
addNotify in class Component (II-§1.10.2).

countItems

public int countItems()
Returns:
the number of menu items in this choice menu.
See Also:
getItem (II-§1.8.5).

getItem

public String getItem(int index)
Parameters:
index - the index
Returns:
the string at the specified index in this choice menu
See Also:
countItems (II-§1.8.4).

getSelectedIndex

public int getSelectedIndex()
Returns:
the index of the currently selected item in this choice menu.
See Also:
getSelectedItem (II-§1.8.7).

getSelectedItem

public String getSelectedItem()
Returns:
a string representation of the currently selected item in this choice menu.
See Also:
getSelectedIndex (II-§1.8.6).

paramString

protected String paramString()
Returns the parameter string representing the state of this choice menu. This string is useful for debugging.
Returns:
the parameter string of this choice menu.
Overrides:
paramString in class Component (II-§1.10.51).

select

public void select(int pos)
Sets the selected item in this choice menu to be the item at the specified position.
Parameters:
pos - the selected item position
Throws
IllegalArgumentException (I-§1.32)
If the choice item position is invalid.
See Also:
getSelectedItem (II-§1.8.7)
getSelectedIndex (II-§1.8.6).

select

public void select(String str)
Sets the selected item in this choice menu to be the choice whose name is equal (I-§1.16.14) to the specified string. If more than one choice is equal to the specified string, the one with the smallest index whose name matches is selected.

Parameters:
str - the string to select
See Also:
getSelectedItem (II-§1.8.7)
getSelectedIndex (II-§1.8.6).

1 In Java 1.0, the AWT does not send mouse or focus events to a choice menu. In Java 1.1, the AWT sends the cheoice menu all mouse, keyboard, and focus events that occur over it.

Packages  This Package  Prev  Next  Index
Java API Document (HTML generated by dkramer on April 22, 1996)
Copyright © 1996 Sun Microsystems, Inc. All rights reserved
Please send any comments or corrections to doug.kramer@sun.com