Class JXComboBox

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

public class JXComboBox extends JComboBox
An enhanced JComboBox that provides the following additional functionality:

Auto-starts edits correctly for AutoCompletion when inside a JTable. A normal JComboBox fails to recognize the first key stroke when it has been decorated.

Adds highlighting support.

See Also:
  • Field Details

  • Constructor Details

    • JXComboBox

      public JXComboBox()
      Creates a JXComboBox with a default data model. The default data model is an empty list of objects. Use addItem to add items. By default the first item in the data model becomes selected.
      See Also:
    • JXComboBox

      public JXComboBox(ComboBoxModel model)
      Creates a JXComboBox that takes its items from an existing ComboBoxModel. Since the ComboBoxModel is provided, a combo box created using this constructor does not create a default combo box model and may impact how the insert, remove and add methods behave.
      Parameters:
      model - the ComboBoxModel that provides the displayed list of items
      See Also:
    • JXComboBox

      public JXComboBox(Object[] items)
      Creates a JXComboBox that contains the elements in the specified array. By default the first item in the array (and therefore the data model) becomes selected.
      Parameters:
      items - an array of objects to insert into the combo box
      See Also:
    • JXComboBox

      public JXComboBox(Vector<?> items)
      Creates a JXComboBox that contains the elements in the specified Vector. By default the first item in the vector (and therefore the data model) becomes selected.
      Parameters:
      items - an array of vectors to insert into the combo box
      See Also:
  • Method Details

    • init

      private void init()
    • getPopupListFor

      protected static JList getPopupListFor(JComboBox comboBox)
    • createDefaultKeySelectionManager

      protected JComboBox.KeySelectionManager createDefaultKeySelectionManager()

      This implementation uses the StringValue representation of the elements to determine the selected item.

      Overrides:
      createDefaultKeySelectionManager in class JComboBox
    • processKeyBinding

      protected boolean processKeyBinding(KeyStroke ks, KeyEvent e, int condition, boolean pressed)
      Overrides:
      processKeyBinding in class JComboBox
    • isStartingCellEdit

      private boolean isStartingCellEdit(KeyEvent e)
    • getComponentAdapter

      protected ComponentAdapter getComponentAdapter()
      Returns:
      the unconfigured ComponentAdapter.
    • getComponentAdapter

      protected ComponentAdapter getComponentAdapter(int index)
      Convenience to access a configured ComponentAdapter. Note: the column index of the configured adapter is always 0.
      Parameters:
      index - the row index in view coordinates, must be valid.
      Returns:
      the configured ComponentAdapter.
    • getStringValueRegistry

      protected StringValueRegistry getStringValueRegistry()
      Returns the StringValueRegistry which defines the string representation for each cells. This is strictly for internal use by the table, which has the responsibility to keep in synch with registered renderers.

      Currently exposed for testing reasons, client code is recommended to not use nor override.

      Returns:
      the current string value registry
    • createDefaultStringValueRegistry

      protected StringValueRegistry createDefaultStringValueRegistry()
      Creates and returns the default registry for StringValues.

      Returns:
      the default registry for StringValues.
    • getStringAt

      public String getStringAt(int row)
      Returns the string representation of the cell value at the given position.
      Parameters:
      row - the row index of the cell in view coordinates
      Returns:
      the string representation of the cell value as it will appear in the table.
    • getDelegatingRenderer

      private JXComboBox.DelegatingRenderer getDelegatingRenderer()
    • createDefaultCellRenderer

      protected ListCellRenderer createDefaultCellRenderer()
      Creates and returns the default cell renderer to use. Subclasses may override to use a different type. Here: returns a DefaultListRenderer.
      Returns:
      the default cell renderer to use with this list.
    • getRenderer

      public ListCellRenderer getRenderer()

      Overridden to return the delegating renderer which is wrapped around the original to support highlighting. The returned renderer is of type DelegatingRenderer and guaranteed to not-null

      Overrides:
      getRenderer in class JComboBox
      See Also:
    • getWrappedRenderer

      public ListCellRenderer getWrappedRenderer()
      Returns the renderer installed by client code or the default if none has been set.
      Returns:
      the wrapped renderer.
      See Also:
    • setRenderer

      public void setRenderer(ListCellRenderer renderer)

      Overridden to wrap the given renderer in a DelegatingRenderer to support highlighting.

      Note: the wrapping implies that the renderer returned from the getCellRenderer is not the renderer as given here, but the wrapper. To access the original, use getWrappedCellRenderer.

      Overrides:
      setRenderer in class JComboBox
      See Also:
    • isUseHighlightersForCurrentValue

      public boolean isUseHighlightersForCurrentValue()
      PENDING JW to KS: review method naming - doesn't sound like valid English to me (no native speaker of course :-). Options are to change the property name to usingHighlightersForCurrentValue (as we did in JXMonthView after some debate) or stick to getXX. Thinking about it: maybe then the property should be usesHighlightersXX, that is third person singular instead of imperative, like in tracksVerticalViewport of JTable?
      Returns:
      true if the combo box decorates the current value with highlighters; false otherwise
    • setUseHighlightersForCurrentValue

      public void setUseHighlightersForCurrentValue(boolean useHighlightersForCurrentValue)
    • getCompoundHighlighter

      private CompoundHighlighter getCompoundHighlighter()
      Returns the CompoundHighlighter assigned to the table, null if none. PENDING: open up for subclasses again?.
      Returns:
      the CompoundHighlighter assigned to the table.
      See Also:
    • setCompoundHighlighter

      private void setCompoundHighlighter(CompoundHighlighter pipeline)
      Assigns a CompoundHighlighter to the table, maybe null to remove all Highlighters.

      The default value is null.

      PENDING: open up for subclasses again?.

      Parameters:
      pipeline - the CompoundHighlighter to use for renderer decoration.
      See Also:
    • setHighlighters

      public void setHighlighters(Highlighter... highlighters)
      Sets the Highlighters to the column, replacing any old settings. None of the given Highlighters must be null.

      Parameters:
      highlighters - zero or more not null highlighters to use for renderer decoration.
      See Also:
    • getHighlighters

      public Highlighter[] getHighlighters()
      Returns the Highlighters used by this column. Maybe empty, but guarantees to be never null.
      Returns:
      the Highlighters used by this column, guaranteed to never null.
      See Also:
    • addHighlighter

      public void addHighlighter(Highlighter highlighter)
      Adds a Highlighter. Appends to the end of the list of used Highlighters.

      Parameters:
      highlighter - the Highlighter to add.
      Throws:
      NullPointerException - if Highlighter is null.
      See Also:
    • removeHighlighter

      public void removeHighlighter(Highlighter highlighter)
      Removes the given Highlighter.

      Does nothing if the Highlighter is not contained.

      Parameters:
      highlighter - the Highlighter to remove.
      See Also:
    • getHighlighterChangeListener

      protected ChangeListener getHighlighterChangeListener()
      Returns the ChangeListener to use with highlighters. Lazily creates the listener.
      Returns:
      the ChangeListener for observing changes of highlighters, guaranteed to be not-null
    • createHighlighterChangeListener

      protected ChangeListener createHighlighterChangeListener()
      Creates and returns the ChangeListener observing Highlighters.

      A property change event is create for a state change.

      Returns:
      the ChangeListener defining the reaction to changes of highlighters.
    • updateUI

      public void updateUI()

      Overridden to update renderer and highlighters.

      Overrides:
      updateUI in class JComboBox