net.mariottini.swing
Class JColorComboBox

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 net.mariottini.swing.JColorComboBox
All Implemented Interfaces:
ActionListener, ImageObserver, ItemSelectable, MenuContainer, Serializable, EventListener, Accessible, ListDataListener

public final class JColorComboBox
extends JComboBox

A combo box that lets the user choose a color. The appaerance of the combo box is fully customizable:
It can show colored lines, rectangles or text:

LineRectText
Closed:
Open:

You can have also lines or rectangles with text:
LineRect

License:
  Copyright © 2006, 2007 Roberto Mariottini. All rights reserved.

  Permission is granted to anyone to use this software in source and binary forms
  for any purpose, with or without modification, including commercial applications,
  and to alter it and redistribute it freely, provided that the following conditions
  are met:

  o  Redistributions of source code must retain the above copyright notice,
     this list of conditions and the following disclaimer.
  o  The origin of this software must not be misrepresented; you must not
     claim that you wrote the original software. If you use this software
     in a product, an acknowledgment in the product documentation would be
     appreciated but is not required.
  o  Altered source versions must be plainly marked as such, and must not
     be misrepresented as being the original software.
 
  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
  FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  SUCH DAMAGE.
 

See Also:
Serialized Form

Nested Class Summary
 
Nested classes/interfaces inherited from class javax.swing.JComboBox
JComboBox.KeySelectionManager
 
Nested classes/interfaces inherited from class javax.swing.JComponent
JComponent.AccessibleJComponent
 
Field Summary
static int LINE
          Show the color with a colored line.
static int RECT
          Show the color with a colored rectangle.
static int TEXT_ONLY
          Show the color with a colored string.
 
Fields inherited from class javax.swing.JComponent
TOOL_TIP_TEXT_KEY, 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
JColorComboBox(int type)
          Create a color combo box of the specified type.
JColorComboBox(int type, boolean showText)
          Create a color combo box of the specified type, optionally showing also colors names.
JColorComboBox(int type, Color[] colors)
          Create a color combo box of the specified type, offering the specified colors.
JColorComboBox(int type, Color[] colors, String[] colorNames)
          Create a color combo box of the specified type, offering the specified colors and color names.
 
Method Summary
 int getColorThickness()
          Returns the current color line thickness/rectangle height.
 int getColorWidth()
          Returns the width of the color line/rectangle.
 int getItemHeight()
          Returns the current height of the combo box list item.
 Color getSelectedColor()
          Returns the selected color.
 boolean getShowText()
          Returns the text showing status.
 int getType()
          Returns the type of this combo box.
 void setColorThickness(int thickness)
          Set the thickness of the color line (if type is LINE) or the height of the color rectangle (if type is RECT).
 void setColorWidth(int width)
          Sets the width of the color line/rectangle.
 void setFont(Font font)
          Sets the font to use for this combo box.
 void setItemHeight(int height)
          Sets the height of the combo box list item.
 void setSelectedColor(Color color)
          Select a color.
 void setSelectedItem(Object color)
          Select a color.
 void setShowText(boolean showText)
          Call this function to enable/disable the showing of colors names.
 void setType(int type)
          Set this combo box of the specified type.
 
Methods inherited from class javax.swing.JComboBox
actionPerformed, addActionListener, addItem, addItemListener, addPopupMenuListener, configureEditor, contentsChanged, firePopupMenuCanceled, firePopupMenuWillBecomeInvisible, firePopupMenuWillBecomeVisible, getAccessibleContext, getAction, getActionCommand, getActionListeners, getEditor, getItemAt, getItemCount, getItemListeners, getKeySelectionManager, getMaximumRowCount, getModel, getPopupMenuListeners, getPrototypeDisplayValue, getRenderer, getSelectedIndex, getSelectedItem, getSelectedObjects, getUI, getUIClassID, hidePopup, insertItemAt, intervalAdded, intervalRemoved, isEditable, isLightWeightPopupEnabled, isPopupVisible, processKeyEvent, removeActionListener, removeAllItems, removeItem, removeItemAt, removeItemListener, removePopupMenuListener, selectWithKeyChar, setAction, setActionCommand, setEditable, setEditor, setEnabled, setKeySelectionManager, setLightWeightPopupEnabled, setMaximumRowCount, setModel, setPopupVisible, setPrototypeDisplayValue, setRenderer, setSelectedIndex, setUI, showPopup, updateUI
 
Methods inherited from class javax.swing.JComponent
addAncestorListener, addNotify, addVetoableChangeListener, computeVisibleRect, contains, createToolTip, disable, enable, firePropertyChange, firePropertyChange, firePropertyChange, getActionForKeyStroke, getActionMap, getAlignmentX, getAlignmentY, getAncestorListeners, getAutoscrolls, getBorder, getBounds, getClientProperty, 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, isPaintingTile, isRequestFocusEnabled, isValidateRoot, paint, paintImmediately, paintImmediately, print, printAll, putClientProperty, registerKeyboardAction, registerKeyboardAction, removeAncestorListener, removeNotify, removeVetoableChangeListener, repaint, repaint, requestDefaultFocus, requestFocus, requestFocus, requestFocusInWindow, resetKeyboardActions, reshape, revalidate, scrollRectToVisible, setActionMap, setAlignmentX, setAlignmentY, setAutoscrolls, setBackground, setBorder, setComponentPopupMenu, setDebugGraphicsOptions, setDefaultLocale, setDoubleBuffered, setFocusTraversalKeys, setForeground, setInheritsPopupMenu, setInputMap, setInputVerifier, setMaximumSize, setMinimumSize, setNextFocusableComponent, setOpaque, setPreferredSize, setRequestFocusEnabled, setToolTipText, setTransferHandler, setVerifyInputWhenFocusTarget, setVisible, unregisterKeyboardAction, update
 
Methods inherited from class java.awt.Container
add, add, add, add, add, addContainerListener, 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, remove, remove, removeAll, removeContainerListener, setComponentZOrder, setFocusCycleRoot, setFocusTraversalPolicy, setFocusTraversalPolicyProvider, setLayout, transferFocusBackward, transferFocusDownCycle, validate
 
Methods inherited from class java.awt.Component
action, add, addComponentListener, addFocusListener, addHierarchyBoundsListener, addHierarchyListener, addInputMethodListener, addKeyListener, addMouseListener, addMouseMotionListener, addMouseWheelListener, bounds, checkImage, checkImage, contains, createImage, createImage, createVolatileImage, createVolatileImage, dispatchEvent, enable, enableInputMethods, 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, 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
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

LINE

public static final int LINE
Show the color with a colored line.

See Also:
Constant Field Values

RECT

public static final int RECT
Show the color with a colored rectangle.

See Also:
Constant Field Values

TEXT_ONLY

public static final int TEXT_ONLY
Show the color with a colored string.

See Also:
Constant Field Values
Constructor Detail

JColorComboBox

public JColorComboBox(int type)
Create a color combo box of the specified type. The colors offered are the predefined colors in the Java class Color. Only graphics (or only text if type is TEXT_ONLY) is shown.

Parameters:
type - one of LINE, RECT or TEXT_ONLY.

JColorComboBox

public JColorComboBox(int type,
                      boolean showText)
Create a color combo box of the specified type, optionally showing also colors names. The colors offered are the predefined colors in the Java class Color.

Parameters:
type - one of LINE, RECT or TEXT_ONLY.
showText - true to show color names also, false to show only color graphics (if type is TEXT_ONLY then this is assumed to be true).

JColorComboBox

public JColorComboBox(int type,
                      Color[] colors)
Create a color combo box of the specified type, offering the specified colors.

Parameters:
type - one of LINE or RECT (TEXT_ONLY is invalid and ignored).
colors - the colors used to fill the combo box.

JColorComboBox

public JColorComboBox(int type,
                      Color[] colors,
                      String[] colorNames)
Create a color combo box of the specified type, offering the specified colors and color names.

Parameters:
type - one of LINE, RECT or TEXT_ONLY.
colors - the colors used to fill the combo box.
colorNames - the color names to use.
Method Detail

setShowText

public void setShowText(boolean showText)
Call this function to enable/disable the showing of colors names.

Parameters:
showText - true to show color names, false to show only color graphics (if the type is TEXT_ONLY then this is assumed to be true).
See Also:
getShowText()

getShowText

public boolean getShowText()
Returns the text showing status.

Returns:
true to if names are shown, false otherwise (if the type is TEXT_ONLY then this will return true).
See Also:
setShowText(boolean)

setType

public void setType(int type)
Set this combo box of the specified type. This will reset any color thickness, color width and item height settings.

Parameters:
type - one of LINE, RECT or TEXT_ONLY.

getType

public int getType()
Returns the type of this combo box.

Returns:
one of LINE, RECT or TEXT_ONLY.
See Also:
setType(int)

setColorThickness

public void setColorThickness(int thickness)
Set the thickness of the color line (if type is LINE) or the height of the color rectangle (if type is RECT). The line thickness can't be less than one, and the retangle height also has an inferior limit. Calling this function will reset the item height.

Parameters:
thickness - the line thickness or the rectangle height.
See Also:
setItemHeight(int)

getColorThickness

public int getColorThickness()
Returns the current color line thickness/rectangle height.

Returns:
the current color line thickness/rectangle height.
See Also:
setColorThickness(int)

setItemHeight

public void setItemHeight(int height)
Sets the height of the combo box list item. This is to leave more space between color lines/rectangles. The height can't be less than the current thickness.

Parameters:
height - the list item height

getItemHeight

public int getItemHeight()
Returns the current height of the combo box list item.

Returns:
the list item height
See Also:
setItemHeight(int)

setColorWidth

public void setColorWidth(int width)
Sets the width of the color line/rectangle. It can't be less than one.

Parameters:
width - the color line/rectangle width.

getColorWidth

public int getColorWidth()
Returns the width of the color line/rectangle.

Returns:
the color line/rectangle width.
See Also:
setColorWidth(int)

setFont

public void setFont(Font font)
Sets the font to use for this combo box. This will change something only if text is enabled.

Overrides:
setFont in class JComponent
Parameters:
font - the font to use to render text
See Also:
setShowText(boolean)

getSelectedColor

public Color getSelectedColor()
Returns the selected color.

Returns:
the currently selected color

setSelectedColor

public void setSelectedColor(Color color)
Select a color.

Parameters:
color - the color to select. If the color is not present in the color list, this function has no effect

setSelectedItem

public void setSelectedItem(Object color)
Select a color.

Overrides:
setSelectedItem in class JComboBox
Parameters:
color - the color to select. If the color is not present in the color list, this function has no effect
See Also:
setSelectedColor(java.awt.Color)


Copyright © 2006, 2007 Roberto Mariottini. All Rights Reserved.