com.mucommander.ui.combobox
Class EditableComboBox

java.lang.Object
  extended by java.awt.Component
      extended by java.awt.Container
          extended by javax.swing.JComponent
              extended by javax.swing.JComboBox
                  extended by com.mucommander.ui.combobox.SaneComboBox
                      extended by com.mucommander.ui.combobox.EditableComboBox
All Implemented Interfaces:
java.awt.event.ActionListener, java.awt.image.ImageObserver, java.awt.ItemSelectable, java.awt.MenuContainer, java.io.Serializable, java.util.EventListener, javax.accessibility.Accessible, javax.swing.event.ListDataListener
Direct Known Subclasses:
AutocompletedEditableCombobox

public class EditableComboBox
extends SaneComboBox

EditableComboBox is an editable combo box (really!) that can use a specified JTextField to be used as the editor.

EditableComboBox also extends JComboBox to make it much easier to use, instead of having to work around its numerous bugs and weird behavior (understatement). Registering a EditableComboBoxListener makes it easy to know for sure when an item has been selected from the combo popup menu, or when the text field has been validated ('Enter' key pressed) or cancelled ('Escape' key pressed). It is strongly recommanded to use this interface instead of ActionListener / ItemListener, their already erratic behavior could be further aggravated by the tweakings used in this class.

The setComboSelectionUpdatesTextField(boolean) method allows to automatically replace the text field's contents when an item is selected from the associated combo box, replacing its value by the selected item's string representation. This feature is disabled by default.

Author:
Maxence Bernard
See Also:
EditableComboBoxListener, Serialized Form

Nested Class Summary
 
Nested classes/interfaces inherited from class javax.swing.JComboBox
javax.swing.JComboBox.AccessibleJComboBox, javax.swing.JComboBox.KeySelectionManager
 
Nested classes/interfaces inherited from class javax.swing.JComponent
javax.swing.JComponent.AccessibleJComponent
 
Nested classes/interfaces inherited from class java.awt.Container
java.awt.Container.AccessibleAWTContainer
 
Nested classes/interfaces inherited from class java.awt.Component
java.awt.Component.AccessibleAWTComponent, java.awt.Component.BaselineResizeBehavior, java.awt.Component.BltBufferStrategy, java.awt.Component.FlipBufferStrategy
 
Field Summary
 
Fields inherited from class javax.swing.JComboBox
actionCommand, dataModel, editor, isEditable, keySelectionManager, lightWeightPopupEnabled, maximumRowCount, selectedItemReminder
 
Fields inherited from class javax.swing.JComponent
accessibleContext, listenerList, TOOL_TIP_TEXT_KEY, ui, UNDEFINED_CONDITION, WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, WHEN_FOCUSED, WHEN_IN_FOCUSED_WINDOW
 
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
EditableComboBox()
          Creates a new editable combo box and a JTextField to be used as the editor.
EditableComboBox(javax.swing.JTextField textField)
          Creates a new editable combo box using the given text field as the editor.
EditableComboBox(javax.swing.JTextField textField, javax.swing.ComboBoxModel comboBoxModel)
          Creates a new editable combo box using the given text field as the editor and ComboBoxModel.
EditableComboBox(javax.swing.JTextField textField, java.lang.Object[] items)
          Creates a new editable combo box using the given text field as the editor and items to populate the initial items list.
EditableComboBox(javax.swing.JTextField textField, java.util.Vector items)
          Creates a new editable combo box using the given text field as the editor and items to populate the initial items list.
 
Method Summary
 void addEditableComboBoxListener(EditableComboBoxListener listener)
          Adds the specified EditableComboBoxListener to the list of registered listeners.
protected  void fireComboBoxSelectionChanged()
          Overrides SaneComboBox.fireComboBoxSelectionChanged() to set the text field's contents to the item that has been selected, if setComboSelectionUpdatesTextField(boolean) has been enabled.
protected  void fireComboFieldCancelled()
          Notifies all registered EditableComboBoxListener instances that the text field has been cancelled, that is the 'Escape' key has been pressed in the text field, without the popup menu being visible.
protected  void fireComboFieldValidated()
          Notifies all registered EditableComboBoxListener instances that the text field has been validated, that is the 'Enter' key has been pressed in the text field, without the popup menu being visible.
 boolean getComboSelectionUpdatesTextField()
          If true is returned, when an item is selected in this combo box, the text field's contents will be automatically replaced by the selected item's string representation.
 javax.swing.JTextField getTextField()
          Returns the text field used as the combo box's editor.
 void removeEditableComboBoxListener(EditableComboBoxListener listener)
          Removes the specified EditableComboBoxListener from the list of registered listeners.
 void setBackground(java.awt.Color color)
           
 void setComboSelectionUpdatesTextField(boolean comboSelectionUpdatesTextField)
          If true is specified, when an item is selected in this combo box, the text field's contents will be automatically replaced by the selected item's string representation.
 void setFont(java.awt.Font font)
           
 void setForeground(java.awt.Color color)
           
 void setSelectionBackground(java.awt.Color color)
           
 void setSelectionForeground(java.awt.Color color)
           
 
Methods inherited from class com.mucommander.ui.combobox.SaneComboBox
addComboBoxListener, addItem, insertItemAt, removeAllItems, removeComboBoxListener, removeItem, removeItemAt
 
Methods inherited from class javax.swing.JComboBox
actionPerformed, actionPropertyChanged, addActionListener, addItemListener, addPopupMenuListener, configureEditor, configurePropertiesFromAction, contentsChanged, createActionPropertyChangeListener, createDefaultKeySelectionManager, fireActionEvent, fireItemStateChanged, firePopupMenuCanceled, firePopupMenuWillBecomeInvisible, firePopupMenuWillBecomeVisible, getAccessibleContext, getAction, getActionCommand, getActionListeners, getEditor, getItemAt, getItemCount, getItemListeners, getKeySelectionManager, getMaximumRowCount, getModel, getPopupMenuListeners, getPrototypeDisplayValue, getRenderer, getSelectedIndex, getSelectedItem, getSelectedObjects, getUI, getUIClassID, hidePopup, installAncestorListener, intervalAdded, intervalRemoved, isEditable, isLightWeightPopupEnabled, isPopupVisible, paramString, processKeyEvent, removeActionListener, removeItemListener, removePopupMenuListener, selectedItemChanged, selectWithKeyChar, setAction, setActionCommand, setEditable, setEditor, setEnabled, setKeySelectionManager, setLightWeightPopupEnabled, setMaximumRowCount, setModel, setPopupVisible, setPrototypeDisplayValue, setRenderer, setSelectedIndex, setSelectedItem, setUI, showPopup, updateUI
 
Methods inherited from class javax.swing.JComponent
addAncestorListener, addNotify, addVetoableChangeListener, computeVisibleRect, contains, createToolTip, disable, enable, firePropertyChange, firePropertyChange, firePropertyChange, fireVetoableChange, getActionForKeyStroke, getActionMap, getAlignmentX, getAlignmentY, getAncestorListeners, getAutoscrolls, getBaseline, getBaselineResizeBehavior, getBorder, getBounds, getClientProperty, getComponentGraphics, getComponentPopupMenu, getConditionForKeyStroke, getDebugGraphicsOptions, getDefaultLocale, getFontMetrics, getGraphics, getHeight, getInheritsPopupMenu, getInputMap, getInputMap, getInputVerifier, getInsets, getInsets, getListeners, getLocation, getMaximumSize, getMinimumSize, getNextFocusableComponent, getPopupLocation, getPreferredSize, getRegisteredKeyStrokes, getRootPane, getSize, getToolTipLocation, getToolTipText, getToolTipText, getTopLevelAncestor, getTransferHandler, getVerifyInputWhenFocusTarget, getVetoableChangeListeners, getVisibleRect, getWidth, getX, getY, grabFocus, isDoubleBuffered, isLightweightComponent, isManagingFocus, isOpaque, isOptimizedDrawingEnabled, isPaintingForPrint, isPaintingTile, isRequestFocusEnabled, isValidateRoot, paint, paintBorder, paintChildren, paintComponent, paintImmediately, paintImmediately, print, printAll, printBorder, printChildren, printComponent, processComponentKeyEvent, processKeyBinding, processMouseEvent, processMouseMotionEvent, putClientProperty, registerKeyboardAction, registerKeyboardAction, removeAncestorListener, removeNotify, removeVetoableChangeListener, repaint, repaint, requestDefaultFocus, requestFocus, requestFocus, requestFocusInWindow, requestFocusInWindow, resetKeyboardActions, reshape, revalidate, scrollRectToVisible, setActionMap, setAlignmentX, setAlignmentY, setAutoscrolls, setBorder, setComponentPopupMenu, setDebugGraphicsOptions, setDefaultLocale, setDoubleBuffered, setFocusTraversalKeys, setInheritsPopupMenu, setInputMap, setInputVerifier, setMaximumSize, setMinimumSize, setNextFocusableComponent, setOpaque, setPreferredSize, setRequestFocusEnabled, setToolTipText, setTransferHandler, setUI, setVerifyInputWhenFocusTarget, setVisible, unregisterKeyboardAction, update
 
Methods inherited from class java.awt.Container
add, add, add, add, add, addContainerListener, addImpl, addPropertyChangeListener, addPropertyChangeListener, applyComponentOrientation, areFocusTraversalKeysSet, countComponents, deliverEvent, doLayout, findComponentAt, findComponentAt, getComponent, getComponentAt, getComponentAt, getComponentCount, getComponents, getComponentZOrder, getContainerListeners, getFocusTraversalKeys, getFocusTraversalPolicy, getLayout, getMousePosition, insets, invalidate, isAncestorOf, isFocusCycleRoot, isFocusCycleRoot, isFocusTraversalPolicyProvider, isFocusTraversalPolicySet, layout, list, list, locate, minimumSize, paintComponents, preferredSize, printComponents, processContainerEvent, processEvent, remove, remove, removeAll, removeContainerListener, setComponentZOrder, setFocusCycleRoot, setFocusTraversalPolicy, setFocusTraversalPolicyProvider, setLayout, transferFocusBackward, transferFocusDownCycle, validate, validateTree
 
Methods inherited from class java.awt.Component
action, add, addComponentListener, addFocusListener, addHierarchyBoundsListener, addHierarchyListener, addInputMethodListener, addKeyListener, addMouseListener, addMouseMotionListener, addMouseWheelListener, bounds, checkImage, checkImage, coalesceEvents, contains, createImage, createImage, createVolatileImage, createVolatileImage, disableEvents, dispatchEvent, enable, enableEvents, enableInputMethods, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, getBackground, getBounds, getColorModel, getComponentListeners, getComponentOrientation, getCursor, getDropTarget, getFocusCycleRootAncestor, getFocusListeners, getFocusTraversalKeysEnabled, getFont, getForeground, getGraphicsConfiguration, getHierarchyBoundsListeners, getHierarchyListeners, getIgnoreRepaint, getInputContext, getInputMethodListeners, getInputMethodRequests, getKeyListeners, getLocale, getLocation, getLocationOnScreen, getMouseListeners, getMouseMotionListeners, getMousePosition, getMouseWheelListeners, getName, getParent, getPeer, getPropertyChangeListeners, getPropertyChangeListeners, getSize, getToolkit, getTreeLock, gotFocus, handleEvent, hasFocus, hide, imageUpdate, inside, isBackgroundSet, isCursorSet, isDisplayable, isEnabled, isFocusable, isFocusOwner, isFocusTraversable, isFontSet, isForegroundSet, isLightweight, isMaximumSizeSet, isMinimumSizeSet, isPreferredSizeSet, isShowing, isValid, isVisible, keyDown, keyUp, list, list, list, location, lostFocus, mouseDown, mouseDrag, mouseEnter, mouseExit, mouseMove, mouseUp, move, nextFocus, paintAll, postEvent, prepareImage, prepareImage, processComponentEvent, processFocusEvent, processHierarchyBoundsEvent, processHierarchyEvent, processInputMethodEvent, processMouseWheelEvent, remove, removeComponentListener, removeFocusListener, removeHierarchyBoundsListener, removeHierarchyListener, removeInputMethodListener, removeKeyListener, removeMouseListener, removeMouseMotionListener, removeMouseWheelListener, removePropertyChangeListener, removePropertyChangeListener, repaint, repaint, repaint, resize, resize, setBounds, setBounds, setComponentOrientation, setCursor, setDropTarget, setFocusable, setFocusTraversalKeysEnabled, setIgnoreRepaint, setLocale, setLocation, setLocation, setName, setSize, setSize, show, show, size, toString, transferFocus, transferFocusUpCycle
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

EditableComboBox

public EditableComboBox()
Creates a new editable combo box and a JTextField to be used as the editor. Has the same effect as calling EditableComboBox(javax.swing.JTextField) with a null value.


EditableComboBox

public EditableComboBox(javax.swing.JTextField textField)
Creates a new editable combo box using the given text field as the editor.

Parameters:
textField - the text field to be used as the combo box's editor. If null, a new JTextField instance will be created and used.

EditableComboBox

public EditableComboBox(javax.swing.JTextField textField,
                        javax.swing.ComboBoxModel comboBoxModel)
Creates a new editable combo box using the given text field as the editor and ComboBoxModel.

Parameters:
textField - the text field to be used as the combo box's editor. If null, a new JTextField instance will be created and used.
comboBoxModel - the ComboBoxModel to use for this combo box

EditableComboBox

public EditableComboBox(javax.swing.JTextField textField,
                        java.lang.Object[] items)
Creates a new editable combo box using the given text field as the editor and items to populate the initial items list.

Parameters:
textField - the text field to be used as the combo box's editor. If null, a new JTextField instance will be created and used.
items - items used to populate the initial items list.

EditableComboBox

public EditableComboBox(javax.swing.JTextField textField,
                        java.util.Vector items)
Creates a new editable combo box using the given text field as the editor and items to populate the initial items list.

Parameters:
textField - the text field to be used as the combo box's editor. If null, a new JTextField instance will be created and used.
items - items used to populate the initial items list.
Method Detail

getTextField

public javax.swing.JTextField getTextField()
Returns the text field used as the combo box's editor.


setComboSelectionUpdatesTextField

public void setComboSelectionUpdatesTextField(boolean comboSelectionUpdatesTextField)
If true is specified, when an item is selected in this combo box, the text field's contents will be automatically replaced by the selected item's string representation.


getComboSelectionUpdatesTextField

public boolean getComboSelectionUpdatesTextField()
If true is returned, when an item is selected in this combo box, the text field's contents will be automatically replaced by the selected item's string representation. This feature is disabled by default (false is returned).


addEditableComboBoxListener

public void addEditableComboBoxListener(EditableComboBoxListener listener)
Adds the specified EditableComboBoxListener to the list of registered listeners.

Listeners are stored as weak references so removeEditableComboBoxListener(EditableComboBoxListener) doesn't need to be called for listeners to be garbage collected when they're not used anymore.

Parameters:
listener - the EditableComboBoxListener to add to the list of registered listeners.

removeEditableComboBoxListener

public void removeEditableComboBoxListener(EditableComboBoxListener listener)
Removes the specified EditableComboBoxListener from the list of registered listeners.

Parameters:
listener - the EditableComboBoxListener to remove from the list of registered listeners.

fireComboBoxSelectionChanged

protected void fireComboBoxSelectionChanged()
Overrides SaneComboBox.fireComboBoxSelectionChanged() to set the text field's contents to the item that has been selected, if setComboSelectionUpdatesTextField(boolean) has been enabled.

Overrides:
fireComboBoxSelectionChanged in class SaneComboBox

fireComboFieldValidated

protected void fireComboFieldValidated()
Notifies all registered EditableComboBoxListener instances that the text field has been validated, that is the 'Enter' key has been pressed in the text field, without the popup menu being visible.

Note: Unlike JComboBox's weird ActionEvent handling, this event is *not* fired when 'Enter' is pressed in the combo popup menu.


fireComboFieldCancelled

protected void fireComboFieldCancelled()
Notifies all registered EditableComboBoxListener instances that the text field has been cancelled, that is the 'Escape' key has been pressed in the text field, without the popup menu being visible.

Note: This event is *not* fired when 'Escape' is pressed in the combo popup menu.


setForeground

public void setForeground(java.awt.Color color)
Overrides:
setForeground in class javax.swing.JComponent

setBackground

public void setBackground(java.awt.Color color)
Overrides:
setBackground in class javax.swing.JComponent

setSelectionForeground

public void setSelectionForeground(java.awt.Color color)

setSelectionBackground

public void setSelectionBackground(java.awt.Color color)

setFont

public void setFont(java.awt.Font font)
Overrides:
setFont in class javax.swing.JComponent


This file is part of muCommander - Copyright (C) 2002-2008 Maxence Bernard