APIdia Launch Partner
Top Description Inners Fields Constructors Methods
javax.swing

public Class JComboBox<E>

extends JComponent
implements ItemSelectable, ListDataListener, ActionListener, Accessible
Class Inheritance
All Implemented Interfaces
javax.accessibility.Accessible, java.awt.event.ActionListener, java.util.EventListener, javax.swing.event.ListDataListener, java.awt.ItemSelectable
Annotations
@JavaBean
defaultProperty:UI
description:A combination of a text field and a drop-down list.
@SwingContainer:false
@SuppressWarnings:serial
Type Parameters
<E>
the type of the elements of this combo box
Imports
java.awt.AWTEvent, .Component, .EventQueue, .IllegalComponentStateException, .ItemSelectable, java.awt.event.ActionEvent, .ActionListener, .InputEvent, .ItemEvent, .ItemListener, .KeyEvent, java.beans.BeanProperty, .JavaBean, .PropertyChangeEvent, .PropertyChangeListener, .Transient, java.io.IOException, .ObjectOutputStream, .Serial, .Serializable, java.util.Locale, .Vector, javax.accessibility.Accessible, .AccessibleAction, .AccessibleComponent, .AccessibleContext, .AccessibleEditableText, .AccessibleIcon, .AccessibleRelationSet, .AccessibleRole, .AccessibleSelection, .AccessibleState, .AccessibleStateSet, .AccessibleTable, .AccessibleText, .AccessibleValue, javax.swing.event.AncestorEvent, .AncestorListener, .EventListenerList, .ListDataEvent, .ListDataListener, .ListSelectionEvent, .ListSelectionListener, .PopupMenuEvent, .PopupMenuListener, javax.swing.plaf.ComboBoxUI

A component that combines a button or editable field and a drop-down list. The user can select a value from the drop-down list, which appears at the user's request. If you make the combo box editable, then the combo box includes an editable field into which the user can type a value.

Warning

Swing is not thread safe. For more information see Swing's Threading Policy.

Warning

Serialized objects of this class will not be compatible with future Swing releases. The current serialization support is appropriate for short term storage or RMI between applications running the same version of Swing. As of 1.4, support for long term storage of all JavaBeans has been added to the java.beans package. Please see java.beans.XMLEncoder.

See How to Use Combo Boxes in The Java Tutorial for further information.

Authors
Arnaud Weber, Mark Davidson
Since
1.2
See Also
ComboBoxModel, DefaultComboBoxModel

Nested and Inner Type Summary

Modifier and TypeClass and Description
protected class
JComboBox.AccessibleJComboBox

This class implements accessibility support for the JComboBox class.

private static class
pack-priv static class
public static interface
JComboBox.KeySelectionManager

The interface that defines a KeySelectionManager.

Field Summary

Modifier and TypeField and Description
private Action
protected String
actionCommand

This protected field is implementation specific.

private PropertyChangeListener
protected ComboBoxModel<E>
dataModel

This protected field is implementation specific.

protected ComboBoxEditor
editor

This protected field is implementation specific.

private boolean
protected boolean
isEditable

This protected field is implementation specific.

protected JComboBox.KeySelectionManager
keySelectionManager

This protected field is implementation specific.

protected boolean
lightWeightPopupEnabled

This protected field is implementation specific.

protected int
maximumRowCount

This protected field is implementation specific.

private E
protected ListCellRenderer<? super E>
renderer

This protected field is implementation specific.

protected Object
selectedItemReminder

This protected field is implementation specific.

private boolean
private static final String
private transient boolean
Inherited from javax.swing.JComponent:
DEBUG_GRAPHICS_LOADEDfocusControllerivSourceComponentlistenerListpaintingChildTOOL_TIP_TEXT_KEYuiUNDEFINED_CONDITIONWHEN_ANCESTOR_OF_FOCUSED_COMPONENTWHEN_FOCUSEDWHEN_IN_FOCUSED_WINDOW

Constructor Summary

AccessConstructor and Description
public
JComboBox(ComboBoxModel<E>
the ComboBoxModel that provides the displayed list of items
aModel
)

Creates a JComboBox that takes its items from an existing ComboBoxModel.

public
JComboBox(E[]
an array of objects to insert into the combo box
items
)

Creates a JComboBox that contains the elements in the specified array.

public
JComboBox(Vector<E>
an array of vectors to insert into the combo box
items
)

Creates a JComboBox that contains the elements in the specified Vector.

public
JComboBox()

Creates a JComboBox with a default data model.

Method Summary

Modifier and TypeMethod and Description
public void
actionPerformed(ActionEvent e)

Implements java.awt.event.ActionListener.actionPerformed.

This method is public as an implementation side effect.

protected void
actionPropertyChanged(Action
the Action associated with this combobox
action
,
String
the name of the property that changed
propertyName
)

Updates the combobox's state in response to property changes in associated action.

public void
addActionListener(ActionListener
the ActionListener that is to be notified
l
)

Adds an ActionListener.

public void
addItem(E
the item to add to the list
item
)

Adds an item to the item list.

public void
addItemListener(ItemListener
the ItemListener that is to be notified
aListener
)

Implements java.awt.ItemSelectable.addItemListener.

Adds an ItemListener.

public void
addPopupMenuListener(PopupMenuListener
the PopupMenuListener to add
l
)

Adds a PopupMenu listener which will listen to notification messages from the popup portion of the combo box.

pack-priv void
checkMutableComboBoxModel()

Checks that the dataModel is an instance of MutableComboBoxModel.

public void
configureEditor(ComboBoxEditor
the ComboBoxEditor that displays the list item in the combo box field and allows it to be edited
anEditor
,
Object
the object to display and edit in the field
anItem
)

Initializes the editor with the specified item.

protected void
configurePropertiesFromAction(Action
the Action from which to get the properties, or null
a
)

Sets the properties on this combobox to match those in the specified Action.

public void
contentsChanged(ListDataEvent e)

Implements javax.swing.event.ListDataListener.contentsChanged.

This method is public as an implementation side effect.

protected PropertyChangeListener

Returns:

the PropertyChangeListener
createActionPropertyChangeListener
(Action
the combobox's action
a
)

Creates and returns a PropertyChangeListener that is responsible for listening for changes from the specified Action and updating the appropriate properties.

protected JComboBox.KeySelectionManager

Returns:

the KeySelectionManager currently used by the list
createDefaultKeySelectionManager
()

Returns an instance of the default key-selection manager.

protected void
fireActionEvent()

Notifies all listeners that have registered interest for notification on this event type.

protected void
fireItemStateChanged(ItemEvent
the event of interest
e
)

Notifies all listeners that have registered interest for notification on this event type.

public void
firePopupMenuCanceled()

Notifies PopupMenuListeners that the popup portion of the combo box has been canceled.

public void
firePopupMenuWillBecomeInvisible()

Notifies PopupMenuListeners that the popup portion of the combo box has become invisible.

public void
firePopupMenuWillBecomeVisible()

Notifies PopupMenuListeners that the popup portion of the combo box will become visible.

public AccessibleContext

Returns:

an AccessibleJComboBox that serves as the AccessibleContext of this JComboBox
getAccessibleContext
()

Overrides java.awt.Component.getAccessibleContext.

Implements javax.accessibility.Accessible.getAccessibleContext.

Gets the AccessibleContext associated with this JComboBox.

public Action

Returns:

the Action for this ActionEvent source; or null
getAction
()

Returns the currently set Action for this ActionEvent source, or null if no Action is set.

public String

Returns:

the string containing the "command" that is sent to action listeners.
getActionCommand
()

Returns the action command that is included in the event sent to action listeners.

public ActionListener[]

Returns:

all of the ActionListeners added or an empty array if no listeners have been added
getActionListeners
()

Returns an array of all the ActionListeners added to this JComboBox with addActionListener().

public ComboBoxEditor

Returns:

the ComboBoxEditor that displays the selected item
getEditor
()

Returns the editor used to paint and edit the selected item in the JComboBox field.

public E

Returns:

the item at that list position; or null if out of range
getItemAt
(int
an integer indicating the list position, where the first item starts at zero
index
)

Returns the list item at the specified index.

public int

Returns:

an integer equal to the number of items in the list
getItemCount
()

Returns the number of items in the list.

public ItemListener[]

Returns:

all of the ItemListeners added or an empty array if no listeners have been added
getItemListeners
()

Returns an array of all the ItemListeners added to this JComboBox with addItemListener().

public JComboBox.KeySelectionManager

Returns:

the KeySelectionManager currently in use
getKeySelectionManager
()

Returns the list's key-selection manager.

public int

Returns:

an integer specifying the maximum number of items that are displayed in the list before using a scrollbar
getMaximumRowCount
()

Returns the maximum number of items the combo box can display without a scrollbar

public ComboBoxModel<E>

Returns:

the ComboBoxModel that provides the displayed list of items
getModel
()

Returns the data model currently used by the JComboBox.

public PopupMenuListener[]

Returns:

all of the PopupMenuListeners added or an empty array if no listeners have been added
getPopupMenuListeners
()

Returns an array of all the PopupMenuListeners added to this JComboBox with addPopupMenuListener().

public E

Returns:

the value of the prototypeDisplayValue property
getPrototypeDisplayValue
()

Returns the "prototypical display" value - an Object used for the calculation of the display height and width.

public ListCellRenderer<? super E>

Returns:

the ListCellRenderer that displays the selected item.
getRenderer
()

Returns the renderer used to display the selected item in the JComboBox field.

public int

Returns:

an integer specifying the currently selected list item, where 0 specifies the first item in the list; or -1 if no item is selected or if the currently selected item is not in the list
getSelectedIndex
()

Returns the first item in the list that matches the given item.

public Object

Returns:

the current selected Object
getSelectedItem
()

Returns the current selected item.

public Object[]

Returns:

an array of Objects containing one element -- the selected item
getSelectedObjects
()

Implements java.awt.ItemSelectable.getSelectedObjects.

Returns an array containing the selected item.

public ComboBoxUI

Returns:

the ComboBoxUI object that renders this component
getUI
()

Overrides javax.swing.JComponent.getUI.

Returns the L&F object that renders this component.

public String

Returns:

the string "ComboBoxUI"
getUIClassID
()

Overrides javax.swing.JComponent.getUIClassID.

Returns the name of the L&F class that renders this component.

public void
hidePopup()

Causes the combo box to close its popup window.

private void
init()

public void
insertItemAt(E
the item to add to the list
item
,
int
an integer specifying the position at which to add the item
index
)

Inserts an item into the item list at a given index.

protected void
installAncestorListener()

Registers ancestor listener so that it will receive AncestorEvents when it or any of its ancestors move or are made visible or invisible.

public void
intervalAdded(ListDataEvent e)

Implements javax.swing.event.ListDataListener.intervalAdded.

This method is public as an implementation side effect.

public void
intervalRemoved(ListDataEvent e)

Implements javax.swing.event.ListDataListener.intervalRemoved.

This method is public as an implementation side effect.

public boolean

Returns:

true if the JComboBox is editable, else false
isEditable
()

Returns true if the JComboBox is editable.

public boolean

Returns:

the value of the lightWeightPopupEnabled property
isLightWeightPopupEnabled
()

Gets the value of the lightWeightPopupEnabled property.

private boolean
public boolean

Returns:

true if the popup is visible, otherwise returns false
isPopupVisible
()

Determines the visibility of the popup.

protected String

Returns:

a string representation of this JComboBox
paramString
()

Overrides javax.swing.JComponent.paramString.

Returns a string representation of this JComboBox.

protected boolean
processKeyBinding(KeyStroke ks, KeyEvent e, int condition, boolean pressed)

Overrides javax.swing.JComponent.processKeyBinding.

Invoked to process the key bindings for ks as the result of the KeyEvent e.

public void
processKeyEvent(KeyEvent
the KeyEvent containing the keyboard key that was pressed
e
)

Overrides javax.swing.JComponent.processKeyEvent.

Handles KeyEvents, looking for the Tab key.

public void
removeActionListener(ActionListener
the ActionListener to remove
l
)

Removes an ActionListener.

public void
removeAllItems()

Removes all items from the item list.

public void
removeItem(Object
the object to remove from the item list
anObject
)

Removes an item from the item list.

public void
removeItemAt(int
an int specifying the index of the item to remove, where 0 indicates the first item in the list
anIndex
)

Removes the item at anIndex This method works only if the JComboBox uses a mutable data model.

public void
removeItemListener(ItemListener
the ItemListener to remove
aListener
)

Implements java.awt.ItemSelectable.removeItemListener.

Removes an ItemListener.

public void
removePopupMenuListener(PopupMenuListener
the PopupMenuListener to remove
l
)

Removes a PopupMenuListener.

protected void
selectedItemChanged()

This protected method is implementation specific.

public boolean

Returns:

true if there is an item corresponding to that character. Otherwise, returns false.
selectWithKeyChar
(char
a char, typically this is a keyboard key typed by the user
keyChar
)

Selects the list item that corresponds to the specified keyboard character and returns true, if there is an item corresponding to that character.

public void
setAction(Action
the Action for the JComboBox, or null.
a
)

Sets the Action for the ActionEvent source.

public void
setActionCommand(String
a string containing the "command" that is sent to action listeners; the same listener can then do different things depending on the command it receives
aCommand
)

Sets the action command that should be included in the event sent to action listeners.

private void
public void
setEditable(boolean
a boolean value, where true indicates that the field is editable
aFlag
)

Determines whether the JComboBox field is editable.

public void
setEditor(ComboBoxEditor
the ComboBoxEditor that displays the selected item
anEditor
)

Sets the editor used to paint and edit the selected item in the JComboBox field.

public void
setEnabled(boolean
a boolean value, where true enables the component and false disables it
b
)

Overrides javax.swing.JComponent.setEnabled.

Enables the combo box so that items can be selected.

public void
setKeySelectionManager(JComboBox.KeySelectionManager
a key selection manager
aManager
)

Sets the object that translates a keyboard character into a list selection.

public void
setLightWeightPopupEnabled(boolean
if true, lightweight popups are desired
aFlag
)

Sets the lightWeightPopupEnabled property, which provides a hint as to whether or not a lightweight Component should be used to contain the JComboBox, versus a heavyweight Component such as a Panel or a Window.

public void
setMaximumRowCount(int
an integer specifying the maximum number of items to display in the list before using a scrollbar
count
)

Sets the maximum number of rows the JComboBox displays.

public void
setModel(ComboBoxModel<E>
the ComboBoxModel that provides the displayed list of items
aModel
)

Sets the data model that the JComboBox uses to obtain the list of items.

public void
setPopupVisible(boolean
if true shows the popup, otherwise, hides the popup.
v
)

Sets the visibility of the popup.

public void
setPrototypeDisplayValue(E
the prototype display value
prototypeDisplayValue
)

Sets the prototype display value used to calculate the size of the display for the UI portion.

public void
setRenderer(ListCellRenderer<? super E>
the ListCellRenderer that displays the selected item
aRenderer
)

Sets the renderer that paints the list items and the item selected from the list in the JComboBox field.

public void
setSelectedIndex(int
an integer specifying the list item to select, where 0 specifies the first item in the list and -1 indicates no selection
anIndex
)

Selects the item at index anIndex.

public void
setSelectedItem(Object
the list object to select; use null to clear the selection
anObject
)

Sets the selected item in the combo box display area to the object in the argument.

public void
setUI(ComboBoxUI
the ComboBoxUI L&F object
ui
)

Sets the L&F object that renders this component.

public void
showPopup()

Causes the combo box to display its popup window.

public void
updateUI()

Overrides javax.swing.JComponent.updateUI.

Resets the UI property to a value from the current look and feel.

private void
writeObject(ObjectOutputStream s)

Hides javax.swing.JComponent.writeObject.

See readObject and writeObject in JComponent for more information about serialization in Swing.

Inherited from javax.swing.JComponent:
_paintImmediatelyaddAncestorListeneraddNotifyaddVetoableChangeListeneralwaysOnTopcheckIfChildObscuredBySiblingclientPropertyChangedcomponentInputMapChangedcomputeVisibleRectcomputeVisibleRectcompWriteObjectNotifycontainscreateToolTipdisabledndDonedropLocationForPointenablefirePropertyChangefirePropertyChangefirePropertyChangefireVetoableChangegetActionForKeyStrokegetActionMapgetActionMapgetAlignmentXgetAlignmentYgetAncestorListenersgetAutoscrollsgetBaselinegetBaselineResizeBehaviorgetBordergetBoundsgetClientPropertygetComponentGraphicsgetComponentPopupMenugetConditionForKeyStrokegetCreatedDoubleBuffergetDebugGraphicsOptionsgetDefaultLocalegetFontMetricsgetGraphicsgetGraphicsInvokedgetHeightgetInheritsPopupMenugetInputMapgetInputMapgetInputMapgetInputVerifiergetInsetsgetInsetsgetListenersgetLocationgetManagingFocusBackwardTraversalKeysgetManagingFocusForwardTraversalKeysgetMaximumSizegetMinimumSizegetNextFocusableComponentgetPopupLocationgetPreferredSizegetRegisteredKeyStrokesgetRootPanegetSizegetToolTipLocationgetToolTipTextgetToolTipTextgetTopLevelAncestorgetTransferHandlergetVerifyInputWhenFocusTargetgetVetoableChangeListenersgetVisibleRectgetWidthgetWriteObjCountergetXgetYgrabFocushideisDoubleBufferedisLightweightComponentisManagingFocusisOpaqueisOptimizedDrawingEnabledisPaintingisPaintingForPrintisPaintingOriginisPaintingTileisRequestFocusEnabledisValidateRootpaintpaintBorderpaintChildrenpaintComponentpaintForceDoubleBufferedpaintImmediatelypaintImmediatelypaintToOffscreenprintprintAllprintBorderprintChildrenprintComponentprocessComponentKeyEventprocessKeyBindingsprocessKeyBindingsForAllComponentsprocessMouseEventprocessMouseMotionEventputClientPropertyrectangleIsObscuredregisterKeyboardActionregisterKeyboardActionremoveAncestorListenerremoveNotifyremoveVetoableChangeListenerrepaintrepaintrequestDefaultFocusrequestFocusrequestFocusrequestFocusInWindowrequestFocusInWindowresetKeyboardActionsreshaperevalidatesafelyGetGraphicssafelyGetGraphicsscrollRectToVisiblesetActionMapsetAlignmentXsetAlignmentYsetAutoscrollssetBackgroundsetBordersetComponentPopupMenusetCreatedDoubleBuffersetDebugGraphicsOptionssetDefaultLocalesetDoubleBufferedsetDropLocationsetFocusTraversalKeyssetFontsetForegroundsetInheritsPopupMenusetInputMapsetInputVerifiersetMaximumSizesetMinimumSizesetNextFocusableComponentsetOpaquesetPaintingChildsetPreferredSizesetRequestFocusEnabledsetToolTipTextsetTransferHandlersetUIsetUIPropertysetVerifyInputWhenFocusTargetsetVisiblesetWriteObjCountershouldDebugGraphicssuperProcessMouseMotionEventunregisterKeyboardActionupdate