Package java.awt |
![]() Previous |
![]() Java API |
![]() Index |
![]() Next |
public class java.awt.Choice extends java.awt.Component { // Constructors public Choice(); // Methods public void addItem(String item); public void addNotify(); public int countItems(); public String getItem(int index); public int getSelectedIndex(); public String getSelectedItem(); protected String paramString(); public void select(int pos); public void select(String str); }
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 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 of the choice menu or of one of its parent containers in order to cause some action to occur.
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 .
public void addItem(String item)Adds an item to this choice menu.
Parameter Description item the item to be added Throw:
If the item's value is equal to null.
public void addNotify()This method calls the createChoice method of this object's toolkit in order to create a ChoicePeer 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 .
public int countItems()Return Value:
Returns the number of menu items in this choice menu.
See Also: getItem .
public String getItem(int index)Return Value:
Returns the string at the specified index in this choice menu
Parameter Description index the index See Also: countItems .
public int getSelectedIndex()Return Value:
Returns the index of the currently selected item in this choice menu.
See Also: getSelectedItem .
public String getSelectedItem()Return Value:
Returns a string representation of the currently selected item in this choice menu.
See Also: getSelectedIndex .
protected String paramString()Returns the parameter string representing the state of this choice menu. This string is useful for debugging.
Return Value:
Returns the parameter string of this choice menu.
Overrides:
paramString in class Component .
public void select(int pos)Sets the selected item in this choice menu to be the item at the specified position.
Parameter Description pos the selected item position Throw:
If the choice item position is invalid.
See Also: getSelectedItem getSelectedIndex .
public void select(String str)Sets the selected item in this choice menu to be the choice whose name is equal 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.
Parameter Description str the string to select See Also: getSelectedItem getSelectedIndex .
(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.