JComboBox Class
Package: javax.swing
Using this creates a combo box, which is a combination of a text field and a drop-down list from which the user can choose a value. If the text field portion of the control is editable, the user can enter a value in the field or edit a value retrieved from the drop-down list. Making the text field uneditable is common, however, and in that case, the user must pick one of the values from the list.
Figure 5-5 shows a simple combo box.
Figure 5-5
Constructors
Constructor |
Description |
|
Creates an empty combo box |
|
Creates a combo box and fills it with the values in the array |
|
Creates a combo box and fills it with the values in the vector |
Methods
Method |
Description |
|
Adds an action listener to the combo box. |
|
Adds the item to the combo box. |
|
Adds an item listener 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. |
|
Returns the selected item. |
|
Inserts an item at a specified index. |
|
Indicates whether the combo box’s text field is editable. |
|
Removes all items from the combo box. |
|
Removes the specified item. |
|
Removes the item at the specified index. |
|
Specifies whether the combo box’s text field is editable. |
|
Sets the number of rows displayed when the combo box list drops down. |
|
Selects the item at the specified index. It throws |
|
Selects the specified item. It throws |
Creating combo boxes
The easiest way to create a combo box is to use the default constructor to create an empty combo box, and then use the addItem
method to add items:
JComboBox combo1 = new JComboBox();
combo1.addItem(“Bashful”);
combo1.addItem(“Doc”);
combo1.addItem(“Dopey”);
combo1.addItem(“Grumpy”);
combo1.addItem(“Happy”);
combo1.addItem(“Sleepy”);
combo1.addItem(“Sneezy”);
Alternatively, you can create a combo box and initialize its contents from an array, as in this example:
String[] theSeven = {“Bashful”, “Doc”, “Dopey”,
“Grumpy”, “Happy”, “Sleepy”, “Sneezy”};
JComboBox combo1 = new JComboBox(theSeven);
If the data you want to display is in an array list or another type of collection, use the toArray
method to convert the collection to an array and then pass the array to the JComboBox
constructor, like so:
JComboBox combo1 = new JComboBox(arraylist1.toArray());
To remove items from the combo box, use one of the remove
methods. If you know the index position of the item you want to remove, call the removeItemAt
method and pass the index number as a parameter. Otherwise, if you have the object you want to remove, call removeItem
and pass the object.
To remove all the items in a combo box, call removeAllItems
.
Getting items from a combo box
To get the item selected by the user, use the getSelectedItem
method. Note that this method returns an Object
type, so you must cast the returned value to the appropriate type before you can use it. For example:
String s = (String)combo1.getSelectedItem();
Here, the getSelectedItem
method retrieves the selected item, casts it to a String
, and saves it in a String
variable named s
.
Handling combo box events
When the user selects an item from a combo box, an action event is generated. In most applications, you simply ignore this event because you usually don’t need to do anything immediately when the user selects an item. Instead, the selected item is processed when the user clicks a button.
If you want to provide immediate feedback when the user selects an item, you can handle the action event in the usual way: Create an ActionListener
that handles the event in an actionPerformed
method and then call the addAction Listener
method of the combo box to add the action listener. The following action listener class displays a message box that reads He’s my favorite too!
if the user picks Dopey:
private class ComboListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
if (e.getSource() == combo1)
{
String s =
(String)combo1.getSelectedItem();
if (s.equals(“Dopey”))
{
JOptionPane.showMessageDialog(
combo1,
“He’s my favorite too!”,
“Good Choice”,
JOptionPane.INFORMATION_MESSAGE);
}
}
}
}