public class

BasicComboBoxUI

extends ComboBoxUI
java.lang.Object
   ↳ javax.swing.plaf.ComponentUI
     ↳ javax.swing.plaf.ComboBoxUI
       ↳ javax.swing.plaf.basic.BasicComboBoxUI
Known Direct Subclasses

Class Overview

Basic UI implementation for JComboBox.

The combo box is a compound component which means that it is an agregate of many simpler components. This class creates and manages the listeners on the combo box and the combo box model. These listeners update the user interface in response to changes in the properties and state of the combo box.

All event handling is handled by listener classes created with the createxxxListener() methods and internal classes. You can change the behavior of this class by overriding the createxxxListener() methods and supplying your own event listeners or subclassing from the ones supplied in this class.

For adding specific actions, overide installKeyboardActions to add actions in response to KeyStroke bindings. See the article Keyboard Bindings in Swing at The Swing Connection.

Summary

Nested Classes
class BasicComboBoxUI.ComboBoxLayoutManager This layout manager handles the 'standard' layout of combo boxes. 
class BasicComboBoxUI.FocusHandler This listener hides the popup when the focus is lost. 
class BasicComboBoxUI.ItemHandler This listener watches for changes to the selection in the combo box. 
class BasicComboBoxUI.KeyHandler This listener checks to see if the key event isn't a navigation key. 
class BasicComboBoxUI.ListDataHandler This listener watches for changes in the ComboBoxModel
class BasicComboBoxUI.PropertyChangeHandler This listener watches for bound properties that have changed in the combo box. 
Fields
protected JButton arrowButton
protected Dimension cachedMinimumSize
protected JComboBox comboBox
protected CellRendererPane currentValuePane
protected Component editor
protected FocusListener focusListener This protected field is implementation specific.
protected boolean hasFocus This protected field is implementation specific.
protected boolean isMinimumSizeDirty
protected ItemListener itemListener This protected field is implementation specific.
protected KeyListener keyListener This protected field is implementation specific.
protected JList listBox
protected ListDataListener listDataListener This protected field is implementation specific.
protected ComboPopup popup
protected KeyListener popupKeyListener
protected MouseListener popupMouseListener
protected MouseMotionListener popupMouseMotionListener
protected PropertyChangeListener propertyChangeListener This protected field is implementation specific.
Public Constructors
BasicComboBoxUI()
Public Methods
void addEditor()
This public method is implementation specific and should be private.
void configureArrowButton()
This public method is implementation specific and should be private.
static ComponentUI createUI(JComponent c)
Accessible getAccessibleChild(JComponent c, int i)
Returns the ith Accessible child of the object.
int getAccessibleChildrenCount(JComponent c)
Returns the number of accessible children in the object.
int getBaseline(JComponent c, int width, int height)
Returns the baseline.
Component.BaselineResizeBehavior getBaselineResizeBehavior(JComponent c)
Returns an enum indicating how the baseline of the component changes as the size changes.
Dimension getMaximumSize(JComponent c)
Returns the specified component's maximum size appropriate for the look and feel.
Dimension getMinimumSize(JComponent c)
The minumum size is the size of the display area plus insets plus the button.
Dimension getPreferredSize(JComponent c)
Returns the specified component's preferred size appropriate for the look and feel.
void installUI(JComponent c)
Configures the specified component appropriate for the look and feel.
boolean isFocusTraversable(JComboBox c)
Determines if the JComboBox is focus traversable.
boolean isPopupVisible(JComboBox c)
Tells if the popup is visible or not.
void paint(Graphics g, JComponent c)
Paints the specified component appropriate for the look and feel.
void paintCurrentValue(Graphics g, Rectangle bounds, boolean hasFocus)
Paints the currently selected item.
void paintCurrentValueBackground(Graphics g, Rectangle bounds, boolean hasFocus)
Paints the background of the currently selected item.
void removeEditor()
This public method is implementation specific and should be private.
void setPopupVisible(JComboBox c, boolean v)
Hides the popup.
void unconfigureArrowButton()
This public method is implementation specific and should be private.
void uninstallUI(JComponent c)
Reverses configuration which was done on the specified component during installUI.
Protected Methods
void configureEditor()
This protected method is implementation specific and should be private.
JButton createArrowButton()
Creates an button which will be used as the control to show or hide the popup portion of the combo box.
ComboBoxEditor createEditor()
Creates the default editor that will be used in editable combo boxes.
FocusListener createFocusListener()
Creates a FocusListener which will be added to the combo box.
ItemListener createItemListener()
Creates an ItemListener which will be added to the combo box.
KeyListener createKeyListener()
Creates a KeyListener which will be added to the combo box.
LayoutManager createLayoutManager()
Creates a layout manager for managing the components which make up the combo box.
ListDataListener createListDataListener()
Creates a list data listener which will be added to the ComboBoxModel.
ComboPopup createPopup()
Creates the popup portion of the combo box.
PropertyChangeListener createPropertyChangeListener()
Creates a PropertyChangeListener which will be added to the combo box.
ListCellRenderer createRenderer()
Creates the default renderer that will be used in a non-editiable combo box.
Dimension getDefaultSize()
Return the default size of an empty display area of the combo box using the current renderer and font.
Dimension getDisplaySize()
Returns the calculated size of the display area.
Insets getInsets()
Gets the insets from the JComboBox.
void installComponents()
Creates and initializes the components which make up the aggregate combo box.
void installDefaults()
Installs the default colors, default font, default renderer, and default editor into the JComboBox.
void installKeyboardActions()
Adds keyboard actions to the JComboBox.
void installListeners()
Create and install the listeners for the combo box and its model.
boolean isNavigationKey(int keyCode)
Returns whether or not the supplied keyCode maps to a key that is used for navigation.
Rectangle rectangleForCurrentValue()
Returns the area that is reserved for drawing the currently selected item.
void selectNextPossibleValue()
Selects the next item in the list.
void selectPreviousPossibleValue()
Selects the previous item in the list.
void toggleOpenClose()
Hides the popup if it is showing and shows the popup if it is hidden.
void unconfigureEditor()
This protected method is implementation specific and should be private.
void uninstallComponents()
The aggregate components which compise the combo box are unregistered and uninitialized.
void uninstallDefaults()
Uninstalls the default colors, default font, default renderer, and default editor into the JComboBox.
void uninstallKeyboardActions()
Removes the focus InputMap and ActionMap.
void uninstallListeners()
Remove the installed listeners from the combo box and its model.
[Expand]
Inherited Methods
From class javax.swing.plaf.ComboBoxUI
From class javax.swing.plaf.ComponentUI
From class java.lang.Object

Fields

protected JButton arrowButton

protected Dimension cachedMinimumSize

protected JComboBox comboBox

protected CellRendererPane currentValuePane

protected Component editor

protected FocusListener focusListener

This protected field is implementation specific. Do not access directly or override. Override the listener construction method instead.

protected boolean hasFocus

This protected field is implementation specific. Do not access directly or override.

protected boolean isMinimumSizeDirty

protected ItemListener itemListener

This protected field is implementation specific. Do not access directly or override. Override the listener construction method instead.

protected KeyListener keyListener

This protected field is implementation specific. Do not access directly or override. Override the listener construction method instead.

protected JList listBox

protected ListDataListener listDataListener

This protected field is implementation specific. Do not access directly or override. Override the listener construction method instead.

protected ComboPopup popup

protected KeyListener popupKeyListener

protected MouseListener popupMouseListener

protected MouseMotionListener popupMouseMotionListener

protected PropertyChangeListener propertyChangeListener

This protected field is implementation specific. Do not access directly or override. Override the listener construction method instead.

Public Constructors

public BasicComboBoxUI ()

Public Methods

public void addEditor ()

This public method is implementation specific and should be private. do not call or override. To implement a specific editor create a custom ComboBoxEditor

public void configureArrowButton ()

This public method is implementation specific and should be private. Do not call or override.

public static ComponentUI createUI (JComponent c)

public Accessible getAccessibleChild (JComponent c, int i)

Returns the ith Accessible child of the object. UIs might need to override this if they present areas on the screen that can be viewed as components, but actual components are not used for presenting those areas.

Note: As of v1.3, it is recommended that developers call Component.AccessibleAWTComponent.getAccessibleChild() instead of this method.

Parameters
i zero-based index of child
Returns
  • the ith Accessible child of the object

public int getAccessibleChildrenCount (JComponent c)

Returns the number of accessible children in the object. If all of the children of this object implement Accessible, this method should return the number of children of this object. UIs might wish to override this if they present areas on the screen that can be viewed as components, but actual components are not used for presenting those areas. Note: As of v1.3, it is recommended that developers call Component.AccessibleAWTComponent.getAccessibleChildrenCount() instead of this method.

Returns
  • the number of accessible children in the object

public int getBaseline (JComponent c, int width, int height)

Returns the baseline.

Parameters
c JComponent baseline is being requested for
width the width to get the baseline for
height the height to get the baseline for
Returns
  • baseline or a value < 0 indicating there is no reasonable baseline

public Component.BaselineResizeBehavior getBaselineResizeBehavior (JComponent c)

Returns an enum indicating how the baseline of the component changes as the size changes.

Parameters
c JComponent to return baseline resize behavior for
Returns
  • an enum indicating how the baseline changes as the component size changes

public Dimension getMaximumSize (JComponent c)

Returns the specified component's maximum size appropriate for the look and feel. If null is returned, the maximum size will be calculated by the component's layout manager instead (this is the preferred approach for any component with a specific layout manager installed). The default implementation of this method invokes getPreferredSize and returns that value.

Parameters
c the component whose maximum size is being queried; this argument is often ignored, but might be used if the UI object is stateless and shared by multiple components
Returns
  • a Dimension object or null

public Dimension getMinimumSize (JComponent c)

The minumum size is the size of the display area plus insets plus the button.

Parameters
c the component whose minimum size is being queried; this argument is often ignored, but might be used if the UI object is stateless and shared by multiple components
Returns
  • a Dimension object or null

public Dimension getPreferredSize (JComponent c)

Returns the specified component's preferred size appropriate for the look and feel. If null is returned, the preferred size will be calculated by the component's layout manager instead (this is the preferred approach for any component with a specific layout manager installed). The default implementation of this method returns null.

Parameters
c the component whose preferred size is being queried; this argument is often ignored, but might be used if the UI object is stateless and shared by multiple components

public void installUI (JComponent c)

Configures the specified component appropriate for the look and feel. This method is invoked when the ComponentUI instance is being installed as the UI delegate on the specified component. This method should completely configure the component for the look and feel, including the following:

  1. Install any default property values for color, fonts, borders, icons, opacity, etc. on the component. Whenever possible, property values initialized by the client program should not be overridden.
  2. Install a LayoutManager on the component if necessary.
  3. Create/add any required sub-components to the component.
  4. Create/install event listeners on the component.
  5. Create/install a PropertyChangeListener on the component in order to detect and respond to component property changes appropriately.
  6. Install keyboard UI (mnemonics, traversal, etc.) on the component.
  7. Initialize any appropriate instance data.

Parameters
c the component where this UI delegate is being installed

public boolean isFocusTraversable (JComboBox c)

Determines if the JComboBox is focus traversable. If the JComboBox is editable this returns false, otherwise it returns true.

public boolean isPopupVisible (JComboBox c)

Tells if the popup is visible or not.

public void paint (Graphics g, JComponent c)

Paints the specified component appropriate for the look and feel. This method is invoked from the ComponentUI.update method when the specified component is being painted. Subclasses should override this method and use the specified Graphics object to render the content of the component.

Parameters
g the Graphics context in which to paint
c the component being painted; this argument is often ignored, but might be used if the UI object is stateless and shared by multiple components

public void paintCurrentValue (Graphics g, Rectangle bounds, boolean hasFocus)

Paints the currently selected item.

public void paintCurrentValueBackground (Graphics g, Rectangle bounds, boolean hasFocus)

Paints the background of the currently selected item.

public void removeEditor ()

This public method is implementation specific and should be private. do not call or override.

See Also

public void setPopupVisible (JComboBox c, boolean v)

Hides the popup.

public void unconfigureArrowButton ()

This public method is implementation specific and should be private. Do not call or override.

public void uninstallUI (JComponent c)

Reverses configuration which was done on the specified component during installUI. This method is invoked when this UIComponent instance is being removed as the UI delegate for the specified component. This method should undo the configuration performed in installUI, being careful to leave the JComponent instance in a clean state (no extraneous listeners, look-and-feel-specific property objects, etc.). This should include the following:

  1. Remove any UI-set borders from the component.
  2. Remove any UI-set layout managers on the component.
  3. Remove any UI-added sub-components from the component.
  4. Remove any UI-added event/property listeners from the component.
  5. Remove any UI-installed keyboard UI from the component.
  6. Nullify any allocated instance data objects to allow for GC.

Parameters
c the component from which this UI delegate is being removed; this argument is often ignored, but might be used if the UI object is stateless and shared by multiple components

Protected Methods

protected void configureEditor ()

This protected method is implementation specific and should be private. do not call or override.

See Also

protected JButton createArrowButton ()

Creates an button which will be used as the control to show or hide the popup portion of the combo box.

Returns
  • a button which represents the popup control

protected ComboBoxEditor createEditor ()

Creates the default editor that will be used in editable combo boxes. A default editor will be used only if an editor has not been explicitly set with setEditor.

Returns
  • a ComboBoxEditor used for the combo box

protected FocusListener createFocusListener ()

Creates a FocusListener which will be added to the combo box. If this method returns null then it will not be added to the combo box.

Returns
  • an instance of a FocusListener or null

protected ItemListener createItemListener ()

Creates an ItemListener which will be added to the combo box. If this method returns null then it will not be added to the combo box.

Subclasses may override this method to return instances of their own ItemEvent handlers.

Returns
  • an instance of an ItemListener or null

protected KeyListener createKeyListener ()

Creates a KeyListener which will be added to the combo box. If this method returns null then it will not be added to the combo box.

Returns
  • an instance KeyListener or null

protected LayoutManager createLayoutManager ()

Creates a layout manager for managing the components which make up the combo box.

Returns
  • an instance of a layout manager

protected ListDataListener createListDataListener ()

Creates a list data listener which will be added to the ComboBoxModel. If this method returns null then it will not be added to the combo box model.

Returns
  • an instance of a ListDataListener or null

protected ComboPopup createPopup ()

Creates the popup portion of the combo box.

Returns
  • an instance of ComboPopup
See Also

protected PropertyChangeListener createPropertyChangeListener ()

Creates a PropertyChangeListener which will be added to the combo box. If this method returns null then it will not be added to the combo box.

Returns
  • an instance of a PropertyChangeListener or null

protected ListCellRenderer createRenderer ()

Creates the default renderer that will be used in a non-editiable combo box. A default renderer will used only if a renderer has not been explicitly set with setRenderer.

Returns
  • a ListCellRender used for the combo box

protected Dimension getDefaultSize ()

Return the default size of an empty display area of the combo box using the current renderer and font.

Returns
  • the size of an empty display area
See Also

protected Dimension getDisplaySize ()

Returns the calculated size of the display area. The display area is the portion of the combo box in which the selected item is displayed. This method will use the prototype display value if it has been set.

For combo boxes with a non trivial number of items, it is recommended to use a prototype display value to significantly speed up the display size calculation.

Returns
  • the size of the display area calculated from the combo box items

protected Insets getInsets ()

Gets the insets from the JComboBox.

protected void installComponents ()

Creates and initializes the components which make up the aggregate combo box. This method is called as part of the UI installation process.

protected void installDefaults ()

Installs the default colors, default font, default renderer, and default editor into the JComboBox.

protected void installKeyboardActions ()

Adds keyboard actions to the JComboBox. Actions on enter and esc are already supplied. Add more actions as you need them.

protected void installListeners ()

Create and install the listeners for the combo box and its model. This method is called when the UI is installed.

protected boolean isNavigationKey (int keyCode)

Returns whether or not the supplied keyCode maps to a key that is used for navigation. This is used for optimizing key input by only passing non- navigation keys to the type-ahead mechanism. Subclasses should override this if they change the navigation keys.

protected Rectangle rectangleForCurrentValue ()

Returns the area that is reserved for drawing the currently selected item.

protected void selectNextPossibleValue ()

Selects the next item in the list. It won't change the selection if the currently selected item is already the last item.

protected void selectPreviousPossibleValue ()

Selects the previous item in the list. It won't change the selection if the currently selected item is already the first item.

protected void toggleOpenClose ()

Hides the popup if it is showing and shows the popup if it is hidden.

protected void unconfigureEditor ()

This protected method is implementation specific and should be private. Do not call or override.

See Also

protected void uninstallComponents ()

The aggregate components which compise the combo box are unregistered and uninitialized. This method is called as part of the UI uninstallation process.

protected void uninstallDefaults ()

Uninstalls the default colors, default font, default renderer, and default editor into the JComboBox.

protected void uninstallKeyboardActions ()

Removes the focus InputMap and ActionMap.

protected void uninstallListeners ()

Remove the installed listeners from the combo box and its model. The number and types of listeners removed and in this method should be the same that was added in installListeners