The JComboBox Class
JComboBox combines a button or editable field and a drop-down list. It is very similar to the AWT Choice component and even implements the ItemSelectable interface for backward compatibility. By default, the JComboBox component provides a single text edit field adjacent to a small button with a downward arrow. When the button is pressed, a pop-up list of choices is displayed, one of which can be selected by the user. If a selection is made, the choice is copied into the edit field, and the pop up disappears. If there was a previous selection, it is erased. You can also remove the pop up by pressing Tab (or Esc, depending on the L&F) while the combo box has the focus.
The text field in the JComboBox component can be either editable or not editable. This state is controlled by the editable property. If the text field is editable, the user is allowed to type information into the text box (which may not correspond to anything in the list), as well as make selections from the list. If the component is not editable, the user can only make selections from the list.
Unless you specify a set of objects in the constructor, the combo box comes up empty. You can use the addItem( ) method to add objects to the combo box list. Conversely, the removeItem( ) and removeItemAt( ) methods remove a specified object from the list. You also have the ability to insert objects at specific locations in the combo box list with the insertItemAt( ) method. If you wish to retrieve the current number of objects in the list, use the getItemCount( ) method, and if you wish to retrieve an object at a specific index, use the getItemAt( ) method.
Note that the list component inside the JComboBox is not part of the component itself but rather part of its UI delegate. Hence, there is no property to access the list component directly. However, you should be able to get any information you need through the component properties or the ComboBoxModel.
As with regular pop-up menus, you have the ability to specify whether the pop up in the JComboBox component should be drawn as a lightweight or a heavyweight component. Lightweight components require less memory and computing resources. However, if you are using any heavyweight components, you should consider forcing the combo box to use a heavyweight pop up, or else the pop up could be obscured behind your heavyweight components. This can be done by setting the lightWeightPopupEnabled property to false. If the property is set to true, the combo box uses a lightweight pop up when appropriate.
The Key Selection Manager
With combo boxes, you have the ability to map keystrokes to item selections in the list. In order to do this, you can create an object that implements the interface JComboBox.KeySelectionManager. This interface contains only one method:
public int selectionForKey(char aKey, ComboBoxModel model)
Invoked by the JComboBox component after receiving a keyboard event while the list pop up is shown. The most recent character pressed, as well as the model for the combo box, is provided. The method must return the index of the list element that should be highlighted in the combo box, or -1 if a selection cannot be determined. Note that this method is equivalent to moving the mouse across the list; hence, if the mouse pointer is anywhere inside the list, this method does not work.
Here is a short code excerpt that uses a key selection manager to map the numerals 0-9 on the keyboard to the first 10 elements in the combo box list:
class myKeySelectionManager implements JComboBox.KeySelectionManager { public int selectionForKey(char aKey, ComboBoxModel aModel) { if ((aKey >= '0') && (aKey <= '9')) return (aKey - '0'); else return -1; } }
You can install the key selection manager using the setKeySelectionManager( ) method of JComboBox:
myComboBox.setKeySelectionManager(new myKeySelectionManager( ));
Example
import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JComboBox; import javax.swing.JFrame; public class EditableComboBox extends JFrame { JComboBox combo = new JComboBox(); public EditableComboBox() { setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); combo.addItem("One"); combo.addItem("Two"); combo.addItem("Three"); combo.addItem("Four"); combo.addItem("Five"); combo.addItem("Six"); combo.setEditable(true); System.out.println("#items=" + combo.getItemCount()); combo.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { System.out.println("Selected index=" + combo.getSelectedIndex() + " Selected item=" + combo.getSelectedItem()); } }); getContentPane().add(combo); pack(); setVisible(true); } public static void main(String arg[]) { new EditableComboBox(); } }