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

Get Java For Dummies Quick Reference now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.