Java Reference
In-Depth Information
The Choice component has several methods used to manipulate items in
Choice lists. Table 5-10 lists several of these methods and their purpose.
Table 5-10
Choice Component Methods
METHOD
SAMPLE
PURPOSE
add()
myDropDown.add(“First Item”);
Populates a drop-down list
Choice()
Choice myDropDown = new Choice();
Serves as constructor method
getIndex()
myDropDown.getIndex();
Returns the index number of the
selected item
getItemCount()
myDropDown.getItemCount();
Returns the number of items in
this Choice menu
getSelectedItem()
String answer =
Returns String of
myDropDown.getSelectedItem();
user selection
insert()
myDropDown.insert(“new item”, 3);
Inserts new item into Choice list at
the specified position
remove()
myDropDown.remove(“String to remove”):
Removes the first occurrence of
myDropDown.remove(2);
the String or removes the item at
the specified position in Choice list
select()
myDropDown.select(“Highlight this one”);
Sets the selected item in this
myDropDown.select(4);
Choice list to be the specified
String or the item at the specified
position
Items added to the Choice component are indexed internally by Java,
beginning with zero and incremented by 1 for each new item. It sometimes is
useful to reference that index number, using the getIndex() method shown in
Table 5-10. Programmers also can access the String of characters displayed in an
item from the list using the getSelectedItem() method.
In the Reservations program, a Choice component will be constructed,
populated with a list of numbers, and then used in the program interface to
allow users to select the number of guests in the party.
 
Search WWH ::




Custom Search