- Direct Known Subclasses:
MetalComboBoxUI
,SynthComboBoxUI
The combo box is a compound component which means that it is an aggregate 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,
override installKeyboardActions
to add actions in response to
KeyStroke bindings. See the article How to Use Key Bindings
-
Nested Class Summary
Modifier and TypeClassDescriptionclass
This layout manager handles the 'standard' layout of combo boxes.class
This listener hides the popup when the focus is lost.class
This listener watches for changes to the selection in the combo box.class
This listener checks to see if the key event isn't a navigation key.class
This listener watches for changes in theComboBoxModel
.class
This listener watches for bound properties that have changed in the combo box. -
Field Summary
Modifier and TypeFieldDescriptionprotected JButton
The arrow button that invokes the popup.protected Dimension
The cached minimum preferred size.The instance ofJComboBox
.protected CellRendererPane
Used to render the currently selected item in the combo box.protected Component
The Component that theComboBoxEditor
uses for editing.protected FocusListener
This protected field is implementation specific.protected boolean
This protected field is implementation specific.protected boolean
The flag for recalculating the minimum preferred size.protected ItemListener
This protected field is implementation specific.protected KeyListener
This protected field is implementation specific.This list is for drawing the current item in the combo box.protected ListDataListener
This protected field is implementation specific.protected Insets
If specified, these insets act as padding around the cell renderer when laying out and painting the "selected" item in the combo box.protected ComboPopup
The implementation ofComboPopup
that is used to show the popup.protected KeyListener
TheKeyListener
listens to events.protected MouseListener
TheMouseListener
listens to events.protected MouseMotionListener
TheMouseMotionListener
listens to events.protected PropertyChangeListener
This protected field is implementation specific.protected boolean
Indicates whether or not the combo box button should be square. -
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionvoid
This public method is implementation specific and should be private.void
This public method is implementation specific and should be private.protected void
This protected method is implementation specific and should be private.protected JButton
Creates a button which will be used as the control to show or hide the popup portion of the combo box.protected ComboBoxEditor
Creates the default editor that will be used in editable combo boxes.protected FocusListener
Creates aFocusListener
which will be added to the combo box.protected ItemListener
Creates anItemListener
which will be added to the combo box.protected KeyListener
Creates aKeyListener
which will be added to the combo box.protected LayoutManager
Creates a layout manager for managing the components which make up the combo box.protected ListDataListener
Creates a list data listener which will be added to theComboBoxModel
.protected ComboPopup
Creates the popup portion of the combo box.protected PropertyChangeListener
Creates aPropertyChangeListener
which will be added to the combo box.protected ListCellRenderer
<Object> Creates the default renderer that will be used in a non-editiable combo box.static ComponentUI
Constructs a new instance ofBasicComboBoxUI
.int
getBaseline
(JComponent c, int width, int height) Returns the baseline.Returns an enum indicating how the baseline of the component changes as the size changes.protected Dimension
Return the default size of an empty display area of the combo box using the current renderer and font.protected Dimension
Returns the calculated size of the display area.protected Insets
Gets the insets from the JComboBox.The minimum size is the size of the display area plus insets plus the button.protected Dimension
getSizeForComponent
(Component comp) Returns the size a component would have if used as a cell renderer.protected void
Creates and initializes the components which make up the aggregate combo box.protected void
Installs the default colors, default font, default renderer, and default editor into the JComboBox.protected void
Adds keyboard actions to the JComboBox.protected void
Creates and installs listeners for the combo box and its model.boolean
isFocusTraversable
(JComboBox<?> c) Determines if the JComboBox is focus traversable.protected boolean
isNavigationKey
(int keyCode) Returns whether or not the supplied keyCode maps to a key that is used for navigation.boolean
isPopupVisible
(JComboBox<?> c) Tells if the popup is visible or not.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.protected Rectangle
Returns the area that is reserved for drawing the currently selected item.void
This public method is implementation specific and should be private.protected void
Selects the next item in the list.protected void
Selects the previous item in the list.void
setPopupVisible
(JComboBox<?> c, boolean v) Hides the popup.protected void
Hides the popup if it is showing and shows the popup if it is hidden.void
This public method is implementation specific and should be private.protected void
This protected method is implementation specific and should be private.protected void
The aggregate components which comprise the combo box are unregistered and uninitialized.protected void
Uninstalls the default colors, default font, default renderer, and default editor from the combo box.protected void
Removes the focus InputMap and ActionMap.protected void
Removes the installed listeners from the combo box and its model.Methods declared in class javax.swing.plaf.ComponentUI
contains, getAccessibleChild, getAccessibleChildrenCount, getMaximumSize, getPreferredSize, installUI, paint, uninstallUI, update
-
Field Details
-
comboBox
The instance ofJComboBox
. -
hasFocus
protected boolean hasFocusThis protected field is implementation specific. Do not access directly or override. -
listBox
This list is for drawing the current item in the combo box. -
currentValuePane
Used to render the currently selected item in the combo box. It doesn't have anything to do with the popup's rendering. -
popup
The implementation ofComboPopup
that is used to show the popup. -
editor
The Component that theComboBoxEditor
uses for editing. -
arrowButton
The arrow button that invokes the popup. -
keyListener
This protected field is implementation specific. Do not access directly or override. Override the listener construction method instead.- See Also:
-
focusListener
This protected field is implementation specific. Do not access directly or override. Override the listener construction method instead.- See Also:
-
propertyChangeListener
This protected field is implementation specific. Do not access directly or override. Override the listener construction method instead.- See Also:
-
itemListener
This protected field is implementation specific. Do not access directly or override. Override the listener construction method instead.- See Also:
-
popupMouseListener
TheMouseListener
listens to events. -
popupMouseMotionListener
TheMouseMotionListener
listens to events. -
popupKeyListener
TheKeyListener
listens to events. -
listDataListener
This protected field is implementation specific. Do not access directly or override. Override the listener construction method instead.- See Also:
-
isMinimumSizeDirty
protected boolean isMinimumSizeDirtyThe flag for recalculating the minimum preferred size. -
cachedMinimumSize
The cached minimum preferred size. -
squareButton
protected boolean squareButtonIndicates whether or not the combo box button should be square. If square, then the width and height are equal, and are both set to the height of the combo minus appropriate insets.- Since:
- 1.7
-
padding
If specified, these insets act as padding around the cell renderer when laying out and painting the "selected" item in the combo box. These insets add to those specified by the cell renderer.- Since:
- 1.7
-
-
Constructor Details
-
BasicComboBoxUI
public BasicComboBoxUI()Constructs aBasicComboBoxUI
.
-
-
Method Details
-
createUI
Constructs a new instance ofBasicComboBoxUI
.- Parameters:
c
- a component- Returns:
- a new instance of
BasicComboBoxUI
-
installDefaults
protected void installDefaults()Installs the default colors, default font, default renderer, and default editor into the JComboBox. -
installListeners
protected void installListeners()Creates and installs listeners for the combo box and its model. This method is called when the UI is installed. -
uninstallDefaults
protected void uninstallDefaults()Uninstalls the default colors, default font, default renderer, and default editor from the combo box. -
uninstallListeners
protected void uninstallListeners()Removes 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 ininstallListeners
-
createPopup
Creates the popup portion of the combo box.- Returns:
- an instance of
ComboPopup
- See Also:
-
createKeyListener
Creates aKeyListener
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
-
createFocusListener
Creates aFocusListener
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
-
createListDataListener
Creates a list data listener which will be added to theComboBoxModel
. If this method returns null then it will not be added to the combo box model.- Returns:
- an instance of a
ListDataListener
or null
-
createItemListener
Creates anItemListener
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
-
createPropertyChangeListener
Creates aPropertyChangeListener
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
-
createLayoutManager
Creates a layout manager for managing the components which make up the combo box.- Returns:
- an instance of a layout manager
-
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 withsetRenderer
.- Returns:
- a
ListCellRender
used for the combo box - See Also:
-
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 withsetEditor
.- Returns:
- a
ComboBoxEditor
used for the combo box - See Also:
-
installComponents
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. -
uninstallComponents
protected void uninstallComponents()The aggregate components which comprise the combo box are unregistered and uninitialized. This method is called as part of the UI uninstallation process. -
addEditor
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 customComboBoxEditor
- See Also:
-
removeEditor
public void removeEditor()This public method is implementation specific and should be private. do not call or override.- See Also:
-
configureEditor
protected void configureEditor()This protected method is implementation specific and should be private. do not call or override.- See Also:
-
unconfigureEditor
protected void unconfigureEditor()This protected method is implementation specific and should be private. Do not call or override.- See Also:
-
configureArrowButton
public void configureArrowButton()This public method is implementation specific and should be private. Do not call or override.- See Also:
-
unconfigureArrowButton
public void unconfigureArrowButton()This public method is implementation specific and should be private. Do not call or override.- See Also:
-
createArrowButton
Creates a 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
-
isPopupVisible
Tells if the popup is visible or not.- Specified by:
isPopupVisible
in classComboBoxUI
- Parameters:
c
- aJComboBox
- Returns:
- true if popup of the
JComboBox
is visible
-
setPopupVisible
Hides the popup.- Specified by:
setPopupVisible
in classComboBoxUI
- Parameters:
c
- aJComboBox
v
- aboolean
determining the visibility of the popup
-
isFocusTraversable
Determines if the JComboBox is focus traversable. If the JComboBox is editable this returns false, otherwise it returns true.- Specified by:
isFocusTraversable
in classComboBoxUI
- Parameters:
c
- aJComboBox
- Returns:
- true if the given
JComboBox
is traversable
-
getMinimumSize
The minimum size is the size of the display area plus insets plus the button.- Overrides:
getMinimumSize
in classComponentUI
- 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 ornull
- See Also:
-
getBaseline
Returns the baseline.- Overrides:
getBaseline
in classComponentUI
- Parameters:
c
-JComponent
baseline is being requested forwidth
- the width to get the baseline forheight
- the height to get the baseline for- Returns:
- baseline or a value < 0 indicating there is no reasonable baseline
- Throws:
NullPointerException
- ifc
isnull
IllegalArgumentException
- if width or height is < 0- Since:
- 1.6
- See Also:
-
getBaselineResizeBehavior
Returns an enum indicating how the baseline of the component changes as the size changes.- Overrides:
getBaselineResizeBehavior
in classComponentUI
- Parameters:
c
-JComponent
to return baseline resize behavior for- Returns:
- an enum indicating how the baseline changes as the component size changes
- Throws:
NullPointerException
- ifc
isnull
- Since:
- 1.6
- See Also:
-
selectNextPossibleValue
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. -
selectPreviousPossibleValue
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. -
toggleOpenClose
protected void toggleOpenClose()Hides the popup if it is showing and shows the popup if it is hidden. -
rectangleForCurrentValue
Returns the area that is reserved for drawing the currently selected item.- Returns:
- the area that is reserved for drawing the currently selected item
-
getInsets
Gets the insets from the JComboBox.- Returns:
- the insets
-
paintCurrentValue
Paints the currently selected item.- Parameters:
g
- an instance ofGraphics
bounds
- a bounding rectangle to render tohasFocus
- is focused
-
paintCurrentValueBackground
Paints the background of the currently selected item.- Parameters:
g
- an instance ofGraphics
bounds
- a bounding rectangle to render tohasFocus
- is focused
-
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:
-
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
- See Also:
-
getSizeForComponent
Returns the size a component would have if used as a cell renderer.- Parameters:
comp
- aComponent
to check- Returns:
- size of the component
- Since:
- 1.7
-
installKeyboardActions
protected void installKeyboardActions()Adds keyboard actions to the JComboBox. Actions on enter and esc are already supplied. Add more actions as you need them. -
uninstallKeyboardActions
protected void uninstallKeyboardActions()Removes the focus InputMap and ActionMap.
-