Package java.awt Previous
Previous
Java API
Java API
Index
Index
Next
Next

Class Choice

Constructors , Methods , Footnotes

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.


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 .


Methods


addItem

public void addItem(String  item) 

Adds an item to this choice menu.

ParameterDescription
item the item to be added

Throw:

NullPointerException

If the item's value is equal to null.


addNotify

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 .


countItems

public int countItems() 

Return Value:

Returns the number of menu items in this choice menu.

See Also: getItem .


getItem

public String getItem(int  index) 

Return Value:

Returns the string at the specified index in this choice menu

ParameterDescription
index the index

See Also: countItems .


getSelectedIndex

public int getSelectedIndex() 

Return Value:

Returns the index of the currently selected item in this choice menu.

See Also: getSelectedItem .


getSelectedItem

public String getSelectedItem() 

Return Value:

Returns a string representation of the currently selected item in this choice menu.

See Also: getSelectedIndex .


paramString

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 .


select

public void select(int  pos) 

Sets the selected item in this choice menu to be the item at the specified position.

ParameterDescription
pos the selected item position

Throw:

IllegalArgumentException

If the choice item position is invalid.

See Also: getSelectedItem getSelectedIndex .


select

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.

ParameterDescription
str the string to select

See Also: getSelectedItem getSelectedIndex .


Footnotes

(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.

Top© 1996 Sun Microsystems, Inc. All rights reserved.