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.
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
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 initializeanItem - 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.
fireActionEvent
protected void fireActionEvent()
This method fires ActionEvent to this JComboBox's registered
ActionListeners. This method is invoked when user explicitly changes
currently selected item.
fireItemStateChanged
protected void fireItemStateChanged(ItemEvent e)
This method fires ItemEvent to this JComboBox's registered ItemListeners.
This method is invoked when currently selected item in this combo box
has changed.
- Parameters:
e - the ItemEvent describing the change in the combo box's
selection.
firePopupMenuCanceled
public void firePopupMenuCanceled()
Fires a popupMenuCanceled() event to all PopupMenuListeners.
Note: This method is intended for use by plaf classes only.
firePopupMenuWillBecomeInvisible
public void firePopupMenuWillBecomeInvisible()
Fires a popupMenuWillBecomeInvisible() event to all
PopupMenuListeners.
Note: This method is intended for use by plaf classes only.
firePopupMenuWillBecomeVisible
public void firePopupMenuWillBecomeVisible()
Fires a popupMenuWillBecomeVisible() event to all
PopupMenuListeners.
Note: This method is intended for use by plaf classes only.
getAction
public Action getAction()
This method returns Action that is invoked when selected item is changed
in the JComboBox.
getActionCommand
public String getActionCommand()
Returns actionCommand associated with the ActionEvent fired by the
JComboBox to its registered ActionListeners.
- Returns:
- String actionCommand for the ActionEvent
getEditor
public ComboBoxEditor getEditor()
Returns editor component that is responsible for displaying/editing
selected item in the combo box.
getItemAt
public Object getItemAt(int index)
Returns elements located in the combo box at the given index.
- Parameters:
index - index specifying location of the component to return.
- Returns:
- component in the combo box that is located in the given index.
getItemCount
public int getItemCount()
This method returns number of elements in this JComboBox
- Returns:
- int number of elements in this JComboBox
getItemListeners
public ItemListener[] getItemListeners()
This method returns array of ItemListeners that are registered with this
JComboBox.
getMaximumRowCount
public int getMaximumRowCount()
This method returns number of rows visible in the JComboBox's list of
items.
- Returns:
- int maximun number of visible rows in the JComboBox's list.
getModel
public ComboBoxModel getModel()
This method returns data model for this comboBox.
- Returns:
- ComboBoxModel containing items for this combo box.
getPrototypeDisplayValue
public Object getPrototypeDisplayValue()
Returns an object that is used as the display value when calculating the
preferred size for the combo box. This value is, of course, never
displayed anywhere.
- Returns:
- The prototype display value (possibly
null).
getSelectedIndex
public int getSelectedIndex()
Returns index of the item that is currently selected in the combo box. If
no item is currently selected, then -1 is returned.
Note: For performance reasons you should minimize invocation of this
method. If the data model is not an instance of
DefaultComboBoxModel the complexity is O(n) where n is the
number of elements in the combo box.
- Returns:
- int Index specifying location of the currently selected item in the
combo box or -1 if nothing is selected in the combo box.
getSelectedItem
public Object getSelectedItem()
Returns currently selected item in the combo box.
The result may be null to indicate that nothing is
currently selected.
- Returns:
- element that is currently selected in this combo box.
getSelectedObjects
public Object[] getSelectedObjects()
Returns Object array of size 1 containing currently selected element in
the JComboBox.
- Specified by:
- getSelectedObjects in interface ItemSelectable
- Returns:
- Object[] Object array of size 1 containing currently selected
element in the JComboBox.
getUI
public ComboBoxUI getUI()
This method returns the UI used to display the JComboBox.
- Returns:
- The UI used to display the JComboBox.
getUIClassID
public String getUIClassID()
This method returns the String identifier for the UI class to the used
with the JComboBox.
- Overrides:
- getUIClassID in interface JComponent
- Returns:
- The String identifier for the UI class.
hidePopup
public void hidePopup()
This method hides popup containing list of combo box's items
insertItemAt
public void insertItemAt(Object element,
int index)
Inserts given element at the specified index to this JComboBox.
A
RuntimeException is thrown if the data model is not
an instance of
MutableComboBoxModel.
- Parameters:
element - element to insertindex - position where to insert the element
intervalAdded
public void intervalAdded(ListDataEvent event)
The part of implementation of ListDataListener interface. This method is
invoked when some items where added to the JComboBox's data model.
- Specified by:
- intervalAdded in interface ListDataListener
- Parameters:
event - ListDataEvent describing the change
intervalRemoved
public void intervalRemoved(ListDataEvent event)
The part of implementation of ListDataListener interface. This method is
invoked when some items where removed from the JComboBox's data model.
- Specified by:
- intervalRemoved in interface ListDataListener
- Parameters:
event - ListDataEvent describing the change.
isEditable
public boolean isEditable()
This method returns true JComboBox is editable and false otherwise
- Returns:
- boolean true if JComboBox is editable and false otherwise
isLightWeightPopupEnabled
public boolean isLightWeightPopupEnabled()
This method returns whether popup menu that is used to display list of
combo box's item is lightWeight or not.
- Returns:
- boolean true if popup menu is lightweight and false otherwise.
isPopupVisible
public boolean isPopupVisible()
Checks if popup is currently visible on the screen.
- Returns:
- boolean true if popup is visible and false otherwise
paramString
protected String paramString()
Returns an implementation-dependent string describing the attributes of
this JComboBox.
- Overrides:
- paramString in interface JComponent
- Returns:
- A string describing the attributes of this
JComboBox
(never null).
processKeyEvent
public void processKeyEvent(KeyEvent e)
This method is fired whenever a key is pressed with the combo box
in focus
- Overrides:
- processKeyEvent in interface JComponent
- Parameters:
e - The KeyEvent indicating which key was pressed.
removeActionListener
public void removeActionListener(ActionListener listener)
This method removes specified ActionListener from this JComboBox.
- Parameters:
listener - ActionListener
removeAllItems
public void removeAllItems()
This method removes all elements from this JComboBox.
A
RuntimeException is thrown if the data model is not an
instance of
MutableComboBoxModel.
removeItem
public void removeItem(Object element)
This method removes given element from this JComboBox.
A
RuntimeException is thrown if the data model is not
an instance of
MutableComboBoxModel.
- Parameters:
element - element to remove
removeItemAt
public void removeItemAt(int index)
This method remove element location in the specified index in the
JComboBox.
A
RuntimeException is thrown if the data model is not
an instance of
MutableComboBoxModel.
- Parameters:
index - index specifying position of the element to remove
removePopupMenuListener
public 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
- Parameters:
listener - to add
selectWithKeyChar
public 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. Otherwise
false is returned.
- Parameters:
keyChar - character indicating which item in the combo box should be
selected.
- Returns:
- boolean true if item corresponding to the specified keyChar
exists in the combo box. Otherwise false is returned.
selectedItemChanged
protected void selectedItemChanged()
This method is invoked whenever selected item changes in the combo box's
data model. It fires ItemEvent and ActionEvent to all registered
ComboBox's ItemListeners and ActionListeners respectively, indicating
the change.
setAction
public void setAction(Action a)
setAction
- Parameters:
a - action to set
setActionCommand
public void setActionCommand(String aCommand)
This method sets actionCommand to the specified string. ActionEvent fired
to this JComboBox registered ActionListeners will contain this
actionCommand.
- Parameters:
aCommand - new action command for the JComboBox's ActionEvent
setEditable
public void setEditable(boolean editable)
This method sets editability of the combo box. If combo box is editable
the user can choose component from the combo box list by typing
component's name in the editor(JTextfield by default). Otherwise if not
editable, the user should use the list to choose the component. This
method fires PropertyChangeEvents to JComboBox's registered
PropertyChangeListeners to indicate that 'editable' property of the
JComboBox has changed.
- Parameters:
editable - indicates if the JComboBox's textfield should be editable
or not.
setEditor
public void setEditor(ComboBoxEditor newEditor)
Sets editor for this JComboBox
- Parameters:
newEditor - ComboBoxEditor for this JComboBox. This method fires
PropertyChangeEvent when 'editor' property is changed.
setEnabled
public void setEnabled(boolean enabled)
This method disables or enables JComboBox. If the JComboBox is enabled,
then user is able to make item choice, otherwise if JComboBox is
disabled then user is not able to make a selection.
- Overrides:
- setEnabled in interface JComponent
- Parameters:
enabled - if 'enabled' is true then enable JComboBox and disable it
setLightWeightPopupEnabled
public void setLightWeightPopupEnabled(boolean enabled)
This method sets JComboBox's popup to be either lightweight or
heavyweight. If 'enabled' is true then lightweight popup is used and
heavyweight otherwise. By default lightweight popup is used to display
this JComboBox's elements.
- Parameters:
enabled - indicates if lightweight popup or heavyweight popup should
be used to display JComboBox's elements.
setMaximumRowCount
public void setMaximumRowCount(int rowCount)
Sets number of rows that should be visible in this JComboBox's popup. If
this JComboBox's popup has more elements that maximum number or rows
then popup will have a scroll pane to allow users to view other
elements.
- Parameters:
rowCount - number of rows that will be visible in JComboBox's popup.
setModel
public void setModel(ComboBoxModel newDataModel)
Set the data model for this JComboBox. This un-registers all listeners
associated with the current model, and re-registers them with the new
model.
- Parameters:
newDataModel - The new data model for this JComboBox
setPopupVisible
public void setPopupVisible(boolean visible)
This method either displayes or hides the popup containing list of combo
box's items.
- Parameters:
visible - show popup if 'visible' is true and hide it otherwise
setPrototypeDisplayValue
public void setPrototypeDisplayValue(Object value)
Sets the object that is assumed to be the displayed item when calculating
the preferred size for the combo box. A
PropertyChangeEvent (with
the name
prototypeDisplayValue) is sent to all registered
listeners.
- Parameters:
value - the new value (null permitted).
setRenderer
public void setRenderer(ListCellRenderer aRenderer)
This method sets cell renderer for this JComboBox that will be used to
paint combo box's items. The Renderer should only be used only when
JComboBox is not editable. In the case when JComboBox is editable the
editor must be used. This method also fires PropertyChangeEvent when
cellRendered for this JComboBox has changed.
- Parameters:
aRenderer - cell renderer that will be used by this JComboBox to
paint its elements.
setSelectedIndex
public void setSelectedIndex(int index)
Forces JComboBox to select component located in the given index in the
combo box.
If the index is below -1 or exceeds the upper bound an
IllegalArgumentException is thrown.
If the index is -1 then no item gets selected.
- Parameters:
index - index specifying location of the component that should be
selected.
setSelectedItem
public void setSelectedItem(Object item)
Forces combo box to select given item
- Parameters:
item - element in the combo box to select.
setUI
public 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.
- Parameters:
ui - The new "UI" property
showPopup
public void showPopup()
This method displays popup with list of combo box's items on the screen
updateUI
public void updateUI()
This method sets this comboBox's UI to the UIManager's default for the
current look and feel.
- Overrides:
- updateUI in interface JComponent
JComboBox.java --
Copyright (C) 2002, 2004, 2005, 2006, Free Software Foundation, Inc.
This file is part of GNU Classpath.
GNU Classpath is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
GNU Classpath is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with GNU Classpath; see the file COPYING. If not, write to the
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA.
Linking this library statically or dynamically with other modules is
making a combined work based on this library. Thus, the terms and
conditions of the GNU General Public License cover the whole
combination.
As a special exception, the copyright holders of this library give you
permission to link this library with independent modules to produce an
executable, regardless of the license terms of these independent
modules, and to copy and distribute the resulting executable under
terms of your choice, provided that you also meet, for each linked
independent module, the terms and conditions of the license of that
module. An independent module is a module which is not derived from
or based on this library. If you modify this library, you may extend
this exception to your version of the library, but you are not
obligated to do so. If you do not wish to do so, delete this
exception statement from your version.