javax.swing

Class JComboBox

Implemented Interfaces:
Accessible, ActionListener, EventListener, ImageObserver, ItemSelectable, ListDataListener, MenuContainer, Serializable

public class JComboBox
extends JComponent
implements ItemSelectable, ListDataListener, ActionListener, Accessible

A component that allows a user to select any item in its list and displays the selected item to the user. JComboBox also can show/hide a popup menu containing its list of item whenever the mouse is pressed over it.
See Also:
Serialized Form

Nested Class Summary

protected class
JComboBox.AccessibleJComboBox
Accessibility support for JComboBox.
static interface
JComboBox.KeySelectionManager
Classes implementing this interface are responsible for matching key characters typed by the user with combo box's items.

Nested classes/interfaces inherited from class javax.swing.JComponent

JComponent.AccessibleJComponent

Nested classes/interfaces inherited from class java.awt.Container

Container.AccessibleAWTContainer

Nested classes/interfaces inherited from class java.awt.Component

Component.AccessibleAWTComponent, Component.BltBufferStrategy, Component.FlipBufferStrategy

Field Summary

protected String
actionCommand
This actionCommand is used in ActionEvent that is fired to JComboBox's ActionListeneres.
protected ComboBoxModel
dataModel
Data model used by JComboBox to keep track of its list data and currently selected element in the list.
protected ComboBoxEditor
editor
Editor that is responsible for editing an object in a combo box list.
protected boolean
isEditable
This field indicates if textfield of this JComboBox is editable or not.
protected JComboBox.KeySelectionManager
keySelectionManager
keySelectionManager
protected boolean
lightWeightPopupEnabled
This property indicates if heavyweight popup or lightweight popup will be used to diplay JComboBox's elements.
protected int
maximumRowCount
Number of rows that will be visible in the JComboBox's popup.
protected ListCellRenderer
renderer
Renderer renders(paints) every object in the combo box list in its associated list cell.
protected Object
selectedItemReminder
This field is reference to the current selection of the combo box.

Fields inherited from class javax.swing.JComponent

TOOL_TIP_TEXT_KEY, UNDEFINED_CONDITION, WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, WHEN_FOCUSED, WHEN_IN_FOCUSED_WINDOW, accessibleContext, listenerList, ui

Fields inherited from class java.awt.Component

BOTTOM_ALIGNMENT, CENTER_ALIGNMENT, LEFT_ALIGNMENT, RIGHT_ALIGNMENT, TOP_ALIGNMENT

Fields inherited from interface java.awt.image.ImageObserver

ABORT, ALLBITS, ERROR, FRAMEBITS, HEIGHT, PROPERTIES, SOMEBITS, WIDTH

Constructor Summary

JComboBox()
Constructor.
JComboBox(Vector itemVector)
Constructs JComboBox object with specified list of items.
JComboBox(Object[] itemArray)
Constructs JComboBox with specified list of items.
JComboBox(ComboBoxModel model)
Constructs JComboBox object with specified data model for it.

Method Summary

void
actionPerformed(ActionEvent e)
This method handles actionEvents fired by the ComboBoxEditor.
void
addActionListener(ActionListener listener)
This methods adds specified ActionListener to this JComboBox.
void
addItem(Object element)
This method adds given element to this JComboBox.
void
addItemListener(ItemListener listener)
This method registers given ItemListener with this JComboBox
void
addPopupMenuListener(PopupMenuListener listener)
Adds PopupMenuListener to combo box to listen to the events fired by the combo box's popup menu containing its list of items
void
configureEditor(ComboBoxEditor anEditor, Object anItem)
This method initializes specified ComboBoxEditor to display given item.
protected void
configurePropertiesFromAction(Action a)
Configure properties of the JComboBox by reading properties of specified action.
void
contentsChanged(ListDataEvent event)
The part of implementation of ListDataListener interface.
protected PropertyChangeListener
createActionPropertyChangeListener(Action action)
Creates PropertyChangeListener to listen for the changes in comboBox's action properties.
protected JComboBox.KeySelectionManager
createDefaultKeySelectionManager()
createDefaultKeySelectionManager
protected void
fireActionEvent()
This method fires ActionEvent to this JComboBox's registered ActionListeners.
protected void
fireItemStateChanged(ItemEvent e)
This method fires ItemEvent to this JComboBox's registered ItemListeners.
void
firePopupMenuCanceled()
Fires a popupMenuCanceled() event to all PopupMenuListeners.
void
firePopupMenuWillBecomeInvisible()
Fires a popupMenuWillBecomeInvisible() event to all PopupMenuListeners.
void
firePopupMenuWillBecomeVisible()
Fires a popupMenuWillBecomeVisible() event to all PopupMenuListeners.
AccessibleContext
getAccessibleContext()
Returns the object that provides accessibility features for this JComboBox component.
Action
getAction()
This method returns Action that is invoked when selected item is changed in the JComboBox.
String
getActionCommand()
Returns actionCommand associated with the ActionEvent fired by the JComboBox to its registered ActionListeners.
ActionListener[]
getActionListeners()
This method returns array of ActionListeners that are registered with this JComboBox.
ComboBoxEditor
getEditor()
Returns editor component that is responsible for displaying/editing selected item in the combo box.
Object
getItemAt(int index)
Returns elements located in the combo box at the given index.
int
getItemCount()
This method returns number of elements in this JComboBox
ItemListener[]
getItemListeners()
This method returns array of ItemListeners that are registered with this JComboBox.
JComboBox.KeySelectionManager
getKeySelectionManager()
getKeySelectionManager
int
getMaximumRowCount()
This method returns number of rows visible in the JComboBox's list of items.
ComboBoxModel
getModel()
This method returns data model for this comboBox.
PopupMenuListener[]
getPopupMenuListeners()
Returns array of PopupMenuListeners that are registered with combo box.
Object
getPrototypeDisplayValue()
Returns an object that is used as the display value when calculating the preferred size for the combo box.
ListCellRenderer
getRenderer()
This method returns renderer responsible for rendering selected item in the combo box
int
getSelectedIndex()
Returns index of the item that is currently selected in the combo box.
Object
getSelectedItem()
Returns currently selected item in the combo box.
Object[]
getSelectedObjects()
Returns Object array of size 1 containing currently selected element in the JComboBox.
ComboBoxUI
getUI()
This method returns the UI used to display the JComboBox.
String
getUIClassID()
This method returns the String identifier for the UI class to the used with the JComboBox.
void
hidePopup()
This method hides popup containing list of combo box's items
void
insertItemAt(Object element, int index)
Inserts given element at the specified index to this JComboBox.
protected void
installAncestorListener()
void
intervalAdded(ListDataEvent event)
The part of implementation of ListDataListener interface.
void
intervalRemoved(ListDataEvent event)
The part of implementation of ListDataListener interface.
boolean
isEditable()
This method returns true JComboBox is editable and false otherwise
boolean
isLightWeightPopupEnabled()
This method returns whether popup menu that is used to display list of combo box's item is lightWeight or not.
boolean
isPopupVisible()
Checks if popup is currently visible on the screen.
protected String
paramString()
Returns an implementation-dependent string describing the attributes of this JComboBox.
void
processKeyEvent(KeyEvent e)
This method is fired whenever a key is pressed with the combo box in focus
void
removeActionListener(ActionListener listener)
This method removes specified ActionListener from this JComboBox.
void
removeAllItems()
This method removes all elements from this JComboBox.
void
removeItem(Object element)
This method removes given element from this JComboBox.
void
removeItemAt(int index)
This method remove element location in the specified index in the JComboBox.
void
removeItemListener(ItemListener listener)
This method unregisters given ItemListener from this JComboBox
void
removePopupMenuListener(PopupMenuListener listener)
Removes PopupMenuListener to combo box to listen to the events fired by the combo box's popup menu containing its list of items
boolean
selectWithKeyChar(char keyChar)
This method selects item in this combo box that matches specified specified keyChar and returns true if such item is found.
protected void
selectedItemChanged()
This method is invoked whenever selected item changes in the combo box's data model.
void
setAction(Action a)
setAction
void
setActionCommand(String aCommand)
This method sets actionCommand to the specified string.
void
setEditable(boolean editable)
This method sets editability of the combo box.
void
setEditor(ComboBoxEditor newEditor)
Sets editor for this JComboBox
void
setEnabled(boolean enabled)
This method disables or enables JComboBox.
void
setKeySelectionManager(JComboBox.KeySelectionManager aManager)
setKeySelectionManager
void
setLightWeightPopupEnabled(boolean enabled)
This method sets JComboBox's popup to be either lightweight or heavyweight.
void
setMaximumRowCount(int rowCount)
Sets number of rows that should be visible in this JComboBox's popup.
void
setModel(ComboBoxModel newDataModel)
Set the data model for this JComboBox.
void
setPopupVisible(boolean visible)
This method either displayes or hides the popup containing list of combo box's items.
void
setPrototypeDisplayValue(Object value)
Sets the object that is assumed to be the displayed item when calculating the preferred size for the combo box.
void
setRenderer(ListCellRenderer aRenderer)
This method sets cell renderer for this JComboBox that will be used to paint combo box's items.
void
setSelectedIndex(int index)
Forces JComboBox to select component located in the given index in the combo box.
void
setSelectedItem(Object item)
Forces combo box to select given item
void
setUI(ComboBoxUI ui)
Set the "UI" property of the combo box, which is a look and feel class responsible for handling comboBox's input events and painting it.
void
showPopup()
This method displays popup with list of combo box's items on the screen
void
updateUI()
This method sets this comboBox's UI to the UIManager's default for the current look and feel.

Methods inherited from class javax.swing.JComponent

addAncestorListener, addNotify, addVetoableChangeListener, computeVisibleRect, contains, createToolTip, disable, enable, extends EventListener> T[] getListeners, firePropertyChange, firePropertyChange, fireVetoableChange, getAccessibleContext, getActionForKeyStroke, getActionMap, getAlignmentX, getAlignmentY, getAncestorListeners, getAutoscrolls, getBorder, getBounds, getClientProperty, getComponentGraphics, getComponentPopupMenu, getConditionForKeyStroke, getDebugGraphicsOptions, getDefaultLocale, getGraphics, getHeight, getInheritsPopupMenu, getInputMap, getInputMap, getInputVerifier, getInsets, getInsets, getLocation, getMaximumSize, getMinimumSize, getNextFocusableComponent, getPreferredSize, getRegisteredKeyStrokes, getRootPane, getSize, getToolTipLocation, getToolTipText, getToolTipText, getTopLevelAncestor, getTransferHandler, getUIClassID, getVerifyInputWhenFocusTarget, getVetoableChangeListeners, getVisibleRect, getWidth, getX, getY, grabFocus, isDoubleBuffered, isLightweightComponent, isManagingFocus, isOpaque, isOptimizedDrawingEnabled, isPaintingTile, isRequestFocusEnabled, isValidateRoot, paint, paintBorder, paintChildren, paintComponent, paintImmediately, paintImmediately, paramString, print, printAll, printBorder, printChildren, printComponent, processComponentKeyEvent, processKeyBinding, processKeyEvent, processMouseMotionEvent, putClientProperty, registerKeyboardAction, registerKeyboardAction, removeAncestorListener, removeNotify, removeVetoableChangeListener, repaint, repaint, requestDefaultFocus, requestFocus, requestFocus, requestFocusInWindow, requestFocusInWindow, resetKeyboardActions, reshape, revalidate, scrollRectToVisible, setActionMap, setAlignmentX, setAlignmentY, setAutoscrolls, setBackground, setBorder, setComponentPopupMenu, setDebugGraphicsOptions, setDefaultLocale, setDoubleBuffered, setEnabled, setFont, setForeground, setInheritsPopupMenu, setInputMap, setInputVerifier, setNextFocusableComponent, setOpaque, setRequestFocusEnabled, setToolTipText, setTransferHandler, setUI, setVerifyInputWhenFocusTarget, setVisible, unregisterKeyboardAction, update, updateUI

Methods inherited from class java.awt.Container

add, add, add, add, add, addContainerListener, addImpl, addNotify, addPropertyChangeListener, addPropertyChangeListener, applyComponentOrientation, areFocusTraversalKeysSet, countComponents, deliverEvent, doLayout, extends EventListener> T[] getListeners, findComponentAt, findComponentAt, getAlignmentX, getAlignmentY, getComponent, getComponentAt, getComponentAt, getComponentCount, getComponentZOrder, getComponents, getContainerListeners, getFocusTraversalKeys, getFocusTraversalPolicy, getInsets, getLayout, getMaximumSize, getMinimumSize, getPreferredSize, insets, invalidate, isAncestorOf, isFocusCycleRoot, isFocusCycleRoot, isFocusTraversalPolicyProvider, isFocusTraversalPolicySet, layout, list, list, locate, minimumSize, paint, paintComponents, paramString, preferredSize, print, printComponents, processContainerEvent, processEvent, remove, remove, removeAll, removeContainerListener, removeNotify, setComponentZOrder, setFocusCycleRoot, setFocusTraversalKeys, setFocusTraversalPolicy, setFocusTraversalPolicyProvider, setFont, setLayout, transferFocusDownCycle, update, validate, validateTree

Methods inherited from class java.awt.Component

action, add, addComponentListener, addFocusListener, addHierarchyBoundsListener, addHierarchyListener, addInputMethodListener, addKeyListener, addMouseListener, addMouseMotionListener, addMouseWheelListener, addNotify, addPropertyChangeListener, addPropertyChangeListener, applyComponentOrientation, areFocusTraversalKeysSet, bounds, checkImage, checkImage, coalesceEvents, contains, contains, createImage, createImage, createVolatileImage, createVolatileImage, deliverEvent, disable, disableEvents, dispatchEvent, doLayout, enable, enable, enableEvents, enableInputMethods, extends EventListener> T[] getListeners, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, getAccessibleContext, getAlignmentX, getAlignmentY, getBackground, getBounds, getBounds, getColorModel, getComponentAt, getComponentAt, getComponentListeners, getComponentOrientation, getCursor, getDropTarget, getFocusCycleRootAncestor, getFocusListeners, getFocusTraversalKeys, getFocusTraversalKeysEnabled, getFont, getFontMetrics, getForeground, getGraphics, getGraphicsConfiguration, getHeight, getHierarchyBoundsListeners, getHierarchyListeners, getIgnoreRepaint, getInputContext, getInputMethodListeners, getInputMethodRequests, getKeyListeners, getLocale, getLocation, getLocation, getLocationOnScreen, getMaximumSize, getMinimumSize, getMouseListeners, getMouseMotionListeners, getMouseWheelListeners, getName, getParent, getPeer, getPreferredSize, getPropertyChangeListeners, getPropertyChangeListeners, getSize, getSize, getToolkit, getTreeLock, getWidth, getX, getY, gotFocus, handleEvent, hasFocus, hide, imageUpdate, inside, invalidate, isBackgroundSet, isCursorSet, isDisplayable, isDoubleBuffered, isEnabled, isFocusCycleRoot, isFocusOwner, isFocusTraversable, isFocusable, isFontSet, isForegroundSet, isLightweight, isMaximumSizeSet, isMinimumSizeSet, isOpaque, isPreferredSizeSet, isShowing, isValid, isVisible, keyDown, keyUp, layout, list, list, list, list, list, locate, location, lostFocus, minimumSize, mouseDown, mouseDrag, mouseEnter, mouseExit, mouseMove, mouseUp, move, nextFocus, paint, paintAll, paramString, postEvent, preferredSize, prepareImage, prepareImage, print, printAll, processComponentEvent, processEvent, processFocusEvent, processHierarchyBoundsEvent, processHierarchyEvent, processInputMethodEvent, processKeyEvent, processMouseEvent, processMouseMotionEvent, processMouseWheelEvent, remove, removeComponentListener, removeFocusListener, removeHierarchyBoundsListener, removeHierarchyListener, removeInputMethodListener, removeKeyListener, removeMouseListener, removeMouseMotionListener, removeMouseWheelListener, removeNotify, removePropertyChangeListener, removePropertyChangeListener, repaint, repaint, repaint, repaint, requestFocus, requestFocus, requestFocusInWindow, requestFocusInWindow, reshape, resize, resize, setBackground, setBounds, setBounds, setComponentOrientation, setCursor, setDropTarget, setEnabled, setFocusTraversalKeys, setFocusTraversalKeysEnabled, setFocusable, setFont, setForeground, setIgnoreRepaint, setLocale, setLocation, setLocation, setMaximumSize, setMinimumSize, setName, setPreferredSize, setSize, setSize, setVisible, show, show, size, toString, transferFocus, transferFocusBackward, transferFocusUpCycle, update, validate

Methods inherited from class java.lang.Object

clone, equals, extends Object> getClass, finalize, hashCode, notify, notifyAll, toString, wait, wait, wait

Field Details

actionCommand

protected String actionCommand
This actionCommand is used in ActionEvent that is fired to JComboBox's ActionListeneres.

dataModel

protected ComboBoxModel dataModel
Data model used by JComboBox to keep track of its list data and currently selected element in the list.

editor

protected ComboBoxEditor editor
Editor that is responsible for editing an object in a combo box list.

isEditable

protected boolean isEditable
This field indicates if textfield of this JComboBox is editable or not.

keySelectionManager

protected JComboBox.KeySelectionManager keySelectionManager
keySelectionManager

lightWeightPopupEnabled

protected boolean lightWeightPopupEnabled
This property indicates if heavyweight popup or lightweight popup will be used to diplay JComboBox's elements.

maximumRowCount

protected int maximumRowCount
Number of rows that will be visible in the JComboBox's popup.

renderer

protected ListCellRenderer renderer
Renderer renders(paints) every object in the combo box list in its associated list cell. This ListCellRenderer is used only when this JComboBox is uneditable.

selectedItemReminder

protected Object selectedItemReminder
This field is reference to the current selection of the combo box.

Constructor Details

JComboBox

public JComboBox()
Constructor. Creates new empty JComboBox. ComboBox's data model is set to DefaultComboBoxModel.

JComboBox

public JComboBox(Vector itemVector)
Constructs JComboBox object with specified list of items.
Parameters:
itemVector - vector containing list of items for this JComboBox.

JComboBox

public JComboBox(Object[] itemArray)
Constructs JComboBox with specified list of items.
Parameters:
itemArray - array containing list of items for this JComboBox

JComboBox

public JComboBox(ComboBoxModel model)
Constructs JComboBox object with specified data model for it.

Note that the JComboBox will not change the value that is preselected by your ComboBoxModel implementation.

Parameters:
model - Data model that will be used by this JComboBox to keep track of its list of items.

Method Details

actionPerformed

public void actionPerformed(ActionEvent e)
This method handles actionEvents fired by the ComboBoxEditor. It changes this JComboBox's selection to the new value currently in the editor and hides list of combo box items.
Specified by:
actionPerformed in interface ActionListener
Parameters:
e - the ActionEvent

addActionListener

public void addActionListener(ActionListener listener)
This methods adds specified ActionListener to this JComboBox.
Parameters:
listener - to add

addItem

public void addItem(Object element)
This method adds given element to this JComboBox.

A RuntimeException is thrown if the data model is not an instance of MutableComboBoxModel.

Parameters:
element - element to add

addItemListener

public void addItemListener(ItemListener listener)
This method registers given ItemListener with this JComboBox
Specified by:
addItemListener in interface ItemSelectable
Parameters:
listener - to remove

addPopupMenuListener

public void addPopupMenuListener(PopupMenuListener listener)
Adds PopupMenuListener to combo box to listen to the events fired by the combo box's popup menu containing its list of items
Parameters:
listener - to add

configureEditor

public void configureEditor(ComboBoxEditor anEditor,
                            Object anItem)
This method initializes specified ComboBoxEditor to display given item.
Parameters:
anEditor - ComboBoxEditor to initialize
anItem - Item that should displayed in the specified editor

configurePropertiesFromAction

protected void configurePropertiesFromAction(Action a)
Configure properties of the JComboBox by reading properties of specified action. This method always sets the comboBox's "enabled" property to the value of the Action's "enabled" property.
Parameters:
a - An Action to configure the combo box from

contentsChanged

public void contentsChanged(ListDataEvent event)
The part of implementation of ListDataListener interface. This method is invoked when contents of the JComboBox's data model changed.
Specified by:
contentsChanged in interface ListDataListener
Parameters:
event - ListDataEvent describing the change

createActionPropertyChangeListener

protected PropertyChangeListener createActionPropertyChangeListener(Action action)
Creates PropertyChangeListener to listen for the changes in comboBox's action properties.
Parameters:
action - action to listen to for property changes
Returns:
a PropertyChangeListener that listens to changes in action properties.

createDefaultKeySelectionManager

protected