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.

9781118239742-fg0505.tif

Figure 5-5

Constructors

Constructor

Description

JComboBox()

Creates an empty combo box

JComboBox(Object[] items)

Creates a combo box and fills it with the values in the array

JComboBox(Vector[] items)

Creates a combo box and fills it with the values in the vector

Methods

Method

Description

void addActionListener(ActionListener listener)

Adds an action listener to the combo box.

void addItem(Object item)

Adds the item to the combo box.

void addItemListener (ItemListener listener)

Adds an item listener to the combo box.

Object getItemAt(int index)

Returns the item at the specified index.

int getItemCount()

Returns the number of items in the combo box.

int getSelectedIndex()

Returns the index of the selected item.

Object getSelectedItem()

Returns the selected item.

void insert ItemAt(Object item, int index)

Inserts an item at a specified index.

Boolean isEditable()

Indicates whether the combo box’s text field is editable.

void removeAllItems()

Removes all items from the combo box.

void removeItem(Object item)

Removes the specified item.

void removeItemAt(int index)

Removes the item at the specified index.

void setEditable (boolean value)

Specifies whether the combo box’s text field is editable.

void setMaximumRow Count(int count)

Sets the number of rows displayed when the combo box list drops down.

void setSelected Index(int index)

Selects the item at the specified index. It throws IllegalArgumentException if the index is less than 0 or greater than the number of items in the combo box.

void setSelected Item(Object item)

Selects the specified item. It throws IllegalArgumentException if the item is not in the combo box.

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());

tip.eps You can add any kind of object you want to a combo box. The combo box calls the toString method of each item to determine the text to display in the drop-down list. Suppose that you have an array of Employee objects. If you create a combo box from this array, the string returned by each employee’s toString method is displayed in the combo box.

tip.eps By default, the user isn’t allowed to edit the data in the text-field portion of the combo box. If you want to allow the user to edit the text field, call setEditable(true). Then the user can type a value that’s not in the combo box.

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);

}

}

}

}

tip.eps Combo boxes also generate item events when the user selects an item. In fact, the combo box generates two item events when the user selects an item, which can be a little confusing. The first event is generated when the previously selected item is deselected. Then, when the new item is selected, another item event is generated. In most cases, you handle combo box action events rather than item events.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset