Java Reference
In-Depth Information
javax.swing.JComponent
javax.swing.JComboBox
+JComboBox()
+JComboBox(items: Object[])
+addItem(item: Object): void
+getItemAt(index: int): Object
+getItemCount(): int
+getSelectedIndex(): int
+setSelectedIndex(index: int): void
+getSelectedItem(): Object
+setSelectedItem(item: Object): void
+removeItem(anObject: Object): void
+removeItemAt(anIndex: int): void
+removeAllItems(): void
Creates a default empty combo box.
Creates a combo box that contains the elements in the specified array.
Adds an item to the combo box.
Returns the item at the specified index.
Returns the number of items in the combo box.
Returns the index of the selected item.
Sets the selected index in the combo box.
Returns the selected item.
Sets the selected item in the combo box.
Removes an item from the item list.
Removes the item at the specified index in the combo box.
Removes all the items in the combo box.
+addActionEvent(listener:
ActionListener): void
+addItemListener(listener:
ItemListener) : void
Adds an
ActionListener
for this object.
Adds an
ItemListener
for this object.
F
IGURE
17.5
JComboBox
enables you to select an item from a set of items.
Listing 17.4 gives a program that lets users view an image and a description of a country's
flag by selecting the country from a combo box, as shown in Figure 17.6.
Combo box
DescriptionPanel
F
IGURE
17.6
Information about a country, including an image and a description of its flag,
is displayed when the country is selected in the combo box.
Here are the major steps in the program:
1. Create the user interface.
Create a combo box with country names as its selection values. Create a
DescriptionPanel
object (the
DescriptionPanel
class was introduced in the pre-
ceding section). Place the combo box in the north of the frame and the description panel
in the center of the frame.
2. Process the event.
Create a listener to implement the
itemStateChanged
handler to set the flag title,
image, and text in the description panel for the selected country name.