SoftNet-Consult Java Utility Library

com.softnetConsult.utils.swing
Class ResizablePopupComboBox

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.softnetConsult.utils.swing.ResizablePopupComboBox
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

public class ResizablePopupComboBox
extends javax.swing.JComboBox

Implements a JComboBox that dynamically adjusts the size of its selection drop-down based on the assumption that all items will be displayed using their toString method.

This product includes software developed by the SoftNet-Consult Java Utility Library project and its contributors.
(http://java-tools.sourceforge.net)
Copyright (c) 2007-2008 SoftNet-Consult.
Copyright (c) 2007-2008 G. Paperin.
All rights reserved.

File: ResizablePopupComboBox.java
Library API version: "2.02"
Java compliance version: "1.5"

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following terms and conditions are met:

1. Redistributions of source code must retain the above acknowledgement of the SoftNet-Consult Java Utility Library project, the above copyright notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above acknowledgement of the SoftNet-Consult Java Utility Library project, the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
3. All advertising materials mentioning features or use of this software or any derived software must display the following acknowledgement:
This product includes software developed by the SoftNet-Consult Java Utility Library project and its contributors.

THIS SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS, CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Version:
"2.02"
Author:
Greg Paperin (http://www.paperin.org)
See Also:
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
private  java.awt.Dimension maxPopupSize
           
private  java.awt.Dimension minPopupSize
           
private  java.awt.Dimension prefPopupSize
           
 
Fields inherited from class javax.swing.JComboBox
actionCommand, dataModel, editor, isEditable, keySelectionManager, lightWeightPopupEnabled, maximumRowCount, renderer, 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
ResizablePopupComboBox()
          Directly calls JComboBox.JComboBox().
ResizablePopupComboBox(javax.swing.ComboBoxModel model)
          Directly calls JComboBox.JComboBox(ComboBoxModel).
ResizablePopupComboBox(java.lang.Object[] items)
          Directly calls JComboBox.JComboBox(Object[]).
ResizablePopupComboBox(java.util.Vector<?> items)
          Directly calls JComboBox.JComboBox(Vector).
 
Method Summary
private  java.awt.Dimension calcPopupSize()
          Gets the actual size for the drop-down popup component of this combo box.
 void firePopupMenuWillBecomeVisible()
           
private  javax.swing.plaf.basic.BasicComboPopup getComboPopup()
          Gets the drop-down popup component for this combo box.
 java.awt.Dimension getMaximumPopupSize()
          Gets the maximum size for this combo box drop-down popup.
 java.awt.Dimension getMinimumPopupSize()
          Gets the minimum size for this combo box drop-down popup.
 void setMaximumPopupSize(java.awt.Dimension dim)
          Sets the maximum size for this combo box drop-down popup.
 void setMaximumPopupSize(int width, int height)
          Sets the maximum size for this combo box drop-down popup.
 void setMinimumPopupSize(java.awt.Dimension dim)
          Sets the minimum size for this combo box drop-down popup.
 void setMinimumPopupSize(int width, int height)
          Sets the minimum size for this combo box drop-down popup.
private  void setPopupSize()
          Calculates the size for the popup of this compo box using calcPopupSize() and sets that as the new drop-down popup size.
 java.awt.Dimension setPreferredPopupSize()
          Gets the preferred size for this combo box drop-down popup.
 void setPreferredPopupSize(java.awt.Dimension dim)
          Sets the preferred size for this combo box drop-down popup.
 void setPreferredPopupSize(int width, int height)
          Sets the preferred size for this combo box drop-down popup.
 void updateUI()
           
 
Methods inherited from class javax.swing.JComboBox
actionPerformed, actionPropertyChanged, addActionListener, addItem, addItemListener, addPopupMenuListener, configureEditor, configurePropertiesFromAction, contentsChanged, createActionPropertyChangeListener, createDefaultKeySelectionManager, fireActionEvent, fireItemStateChanged, firePopupMenuCanceled, firePopupMenuWillBecomeInvisible, getAccessibleContext, getAction, getActionCommand, getActionListeners, getEditor, getItemAt, getItemCount, getItemListeners, getKeySelectionManager, getMaximumRowCount, getModel, getPopupMenuListeners, getPrototypeDisplayValue, getRenderer, getSelectedIndex, getSelectedItem, getSelectedObjects, getUI, getUIClassID, hidePopup, insertItemAt, installAncestorListener, intervalAdded, intervalRemoved, isEditable, isLightWeightPopupEnabled, isPopupVisible, paramString, processKeyEvent, removeActionListener, removeAllItems, removeItem, removeItemAt, removeItemListener, removePopupMenuListener, selectedItemChanged, selectWithKeyChar, setAction, setActionCommand, setEditable, setEditor, setEnabled, setKeySelectionManager, setLightWeightPopupEnabled, setMaximumRowCount, setModel, setPopupVisible, setPrototypeDisplayValue, setRenderer, setSelectedIndex, setSelectedItem, setUI, showPopup
 
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, setBackground, setBorder, setComponentPopupMenu, setDebugGraphicsOptions, setDefaultLocale, setDoubleBuffered, setFocusTraversalKeys, setFont, setForeground, 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
 

Field Detail

minPopupSize

private java.awt.Dimension minPopupSize

maxPopupSize

private java.awt.Dimension maxPopupSize

prefPopupSize

private java.awt.Dimension prefPopupSize
Constructor Detail

ResizablePopupComboBox

public ResizablePopupComboBox()
Directly calls JComboBox.JComboBox().


ResizablePopupComboBox

public ResizablePopupComboBox(javax.swing.ComboBoxModel model)
Directly calls JComboBox.JComboBox(ComboBoxModel).

Parameters:
model - the ComboBoxModel that provides the displayed list of items.

ResizablePopupComboBox

public ResizablePopupComboBox(java.lang.Object[] items)
Directly calls JComboBox.JComboBox(Object[]).

Parameters:
items - an array of objects to insert into the combo box.

ResizablePopupComboBox

public ResizablePopupComboBox(java.util.Vector<?> items)
Directly calls JComboBox.JComboBox(Vector).

Parameters:
items - an vector of objects to insert into the combo box.
Method Detail

setMinimumPopupSize

public void setMinimumPopupSize(int width,
                                int height)
Sets the minimum size for this combo box drop-down popup. Values that are smaller than 0 will be interpreted as unspecified or any.

Parameters:
width - The minimum width of the popup.
height - The minimum height of the popup.

setMinimumPopupSize

public void setMinimumPopupSize(java.awt.Dimension dim)
Sets the minimum size for this combo box drop-down popup. The value null or component values that are smaller than 0 will be interpreted as unspecified or any.

Parameters:
dim - The minimum size for this combo box drop-down popup

getMinimumPopupSize

public java.awt.Dimension getMinimumPopupSize()
Gets the minimum size for this combo box drop-down popup.

Returns:
The minimum size for this combo box drop-down popup. The value null or component values that are smaller than 0 will be interpreted as unspecified or any.

setMaximumPopupSize

public void setMaximumPopupSize(int width,
                                int height)
Sets the maximum size for this combo box drop-down popup. Values that are smaller than 0 will be interpreted as unspecified or any.

Parameters:
width - The maximum width of the popup.
height - The maximum height of the popup.

setMaximumPopupSize

public void setMaximumPopupSize(java.awt.Dimension dim)
Sets the maximum size for this combo box drop-down popup. The value null or component values that are smaller than 0 will be interpreted as unspecified or any.

Parameters:
dim - The maximum size for this combo box drop-down popup

getMaximumPopupSize

public java.awt.Dimension getMaximumPopupSize()
Gets the maximum size for this combo box drop-down popup.

Returns:
The maximum size for this combo box drop-down popup. The value null or component values that are smaller than 0 will be interpreted as unspecified or any.

setPreferredPopupSize

public void setPreferredPopupSize(int width,
                                  int height)
Sets the preferred size for this combo box drop-down popup. Values that are smaller than 0 will be interpreted as unspecified or any.

Parameters:
width - The preferred width of the popup.
height - The preferred height of the popup.

setPreferredPopupSize

public void setPreferredPopupSize(java.awt.Dimension dim)
Sets the preferred size for this combo box drop-down popup. The value null or component values that are smaller than 0 will be interpreted as unspecified or any.

Parameters:
dim - The preferred size for this combo box drop-down popup

setPreferredPopupSize

public java.awt.Dimension setPreferredPopupSize()
Gets the preferred size for this combo box drop-down popup.

Returns:
The preferred size for this combo box drop-down popup. The value null or component values that are smaller than 0 will be interpreted as unspecified or any.

firePopupMenuWillBecomeVisible

public void firePopupMenuWillBecomeVisible()
Overrides:
firePopupMenuWillBecomeVisible in class javax.swing.JComboBox

updateUI

public void updateUI()
Overrides:
updateUI in class javax.swing.JComboBox

getComboPopup

private javax.swing.plaf.basic.BasicComboPopup getComboPopup()
Gets the drop-down popup component for this combo box.

Returns:
The drop-down popup component for this combo box.

calcPopupSize

private java.awt.Dimension calcPopupSize()
Gets the actual size for the drop-down popup component of this combo box. If the size can be determined on the basis of the preferred, minimum and maximum popup size properties, than those values are used. If the size cannot be determined on the basis of those properties because they are set to null or because their width or height components are set to negative values, than the size is determined as follows:
The height is copied from the default dehavious of the popup.
The width is calculated under the assumption that all items of this combo box will be displayed using their .toString() methods. The width of the popup is set to the width of the longest item string under the current font metrics plus the width of the popup scroller it that is visible.

Returns:
The actual size for the drop-down popup component of this combo box.

setPopupSize

private void setPopupSize()
Calculates the size for the popup of this compo box using calcPopupSize() and sets that as the new drop-down popup size.


SoftNet-Consult Java Utility Library is a member of SourceForge.net