javax.swing
Class JColorChooser

java.lang.Object
  |
  +--java.awt.Component
        |
        +--java.awt.Container
              |
              +--javax.swing.JComponent
                    |
                    +--javax.swing.JColorChooser
All Implemented Interfaces:
Accessible, java.awt.image.ImageObserver, MenuContainer, Serializable

public class JColorChooser
extends JComponent
implements Accessible

Safe: JColorChooser provides a pane of controls designed to allow a user to manipulate and select a color. For information about using color choosers, see How to Use Color Choosers, a section in The Java Tutorial.

This class provides three levels of API:

  1. A static convenience method which shows a modal color-chooser dialog and returns the color selected by the user.
  2. A static convenience method for creating a color-chooser dialog where ActionListeners can be specified to be invoked when the user presses one of the dialog buttons.
  3. The ability to create instances of JColorChooser panes directly (within any container). PropertyChange listeners can be added to detect when the current "color" property changes.

Warning: Serialized objects of this class will not be compatible with future Swing releases. The current serialization support is appropriate for short term storage or RMI between applications running the same version of Swing. As of 1.4, support for long term storage of all JavaBeansTM has been added to the java.beans package. Please see java.beans.XMLEncoder.

Version:
1.42 03/20/02
Author:
James Gosling, Amy Fowler, Steve Wilson
See Also:
Serialized Form

Field Summary
protected  AccessibleContext accessibleContext
           
static String CHOOSER_PANELS_PROPERTY
          Suppressed: The chooserPanel array property name.
private  javax.swing.colorchooser.AbstractColorChooserPanel[] chooserPanels
           
private  boolean dragEnabled
           
static String PREVIEW_PANEL_PROPERTY
          Suppressed: The preview panel property name.
private  JComponent previewPanel
           
static String SELECTION_MODEL_PROPERTY
          Suppressed: The selection model property name.
private  javax.swing.colorchooser.ColorSelectionModel selectionModel
           
private static String uiClassID
           
 
Fields inherited from class javax.swing.JComponent
_bounds, listenerList, paintingChild, TOOL_TIP_TEXT_KEY, ui, UNDEFINED_CONDITION, WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, WHEN_FOCUSED, WHEN_IN_FOCUSED_WINDOW
 
Fields inherited from class java.awt.Container
 
Fields inherited from class java.awt.Component
BOTTOM_ALIGNMENT, CENTER_ALIGNMENT, LEFT_ALIGNMENT, RIGHT_ALIGNMENT, TOP_ALIGNMENT
 
Constructor Summary
JColorChooser()
          Enabled: Creates a color chooser pane with an initial color of white.
JColorChooser(Color initialColor)
          Enabled: Creates a color chooser pane with the specified initial color.
JColorChooser(javax.swing.colorchooser.ColorSelectionModel model)
          Enabled: Creates a color chooser pane with the specified ColorSelectionModel.
 
Method Summary
 void addChooserPanel(javax.swing.colorchooser.AbstractColorChooserPanel panel)
          Enabled: Adds a color chooser panel to the color chooser.
static JDialog createDialog(Component c, String title, boolean modal, JColorChooser chooserPane, ActionListener okListener, ActionListener cancelListener)
          Enabled: Creates and returns a new dialog containing the specified ColorChooser pane along with "OK", "Cancel", and "Reset" buttons.
 AccessibleContext getAccessibleContext()
          Suppressed: Gets the AccessibleContext associated with this JColorChooser.
 javax.swing.colorchooser.AbstractColorChooserPanel[] getChooserPanels()
          Enabled: Returns the specified color panels.
 Color getColor()
          Enabled: Gets the current color value from the color chooser.
 boolean getDragEnabled()
          Enabled: Gets the value of the dragEnabled property.
 JComponent getPreviewPanel()
          Enabled: Returns the preview panel that shows a chosen color.
 javax.swing.colorchooser.ColorSelectionModel getSelectionModel()
          Enabled: Returns the data model that handles color selections.
 javax.swing.plaf.ColorChooserUI getUI()
          Suppressed: Returns the L&F object that renders this component.
 String getUIClassID()
          Suppressed: Returns the name of the L&F class that renders this component.
protected  String paramString()
          Returns a string representation of this JColorChooser.
 javax.swing.colorchooser.AbstractColorChooserPanel removeChooserPanel(javax.swing.colorchooser.AbstractColorChooserPanel panel)
          Enabled: Removes the Color Panel specified.
 void setChooserPanels(javax.swing.colorchooser.AbstractColorChooserPanel[] panels)
          Enabled: Specifies the Color Panels used to choose a color value.
 void setColor(Color color)
          Enabled: Sets the current color of the color chooser to the specified color.
 void setColor(int c)
          Enabled: Sets the current color of the color chooser to the specified color.
 void setColor(int r, int g, int b)
          Enabled: Sets the current color of the color chooser to the specified RGB color.
 void setDragEnabled(boolean b)
          Enabled: Sets the dragEnabled property, which must be true to enable automatic drag handling (the first part of drag and drop) on this component.
 void setPreviewPanel(JComponent preview)
          Enabled: Sets the current preview panel.
 void setSelectionModel(javax.swing.colorchooser.ColorSelectionModel newModel)
          Enabled: Sets the model containing the selected color.
 void setUI(javax.swing.plaf.ColorChooserUI ui)
          Suppressed: Sets the L&F object that renders this component.
static Color showDialog(Component component, String title, Color initialColor)
          Enabled: Shows a modal color-chooser dialog and blocks until the dialog is hidden.
 void updateUI()
          Suppressed: Notification from the UIManager that the L&F has changed.
private  void writeObject(ObjectOutputStream s)
          See readObject and writeObject in JComponent for more information about serialization in Swing.
 
Methods inherited from class javax.swing.JComponent
_paintImmediately, addAncestorListener, addNotify, addPropertyChangeListener, addPropertyChangeListener, addVetoableChangeListener, alwaysOnTop, checkIfChildObscuredBySibling, componentInputMapChanged, computeVisibleRect, computeVisibleRect, compWriteObjectNotify, contains, createToolTip, disable, enable, enableSerialization, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, fireVetoableChange, getActionForKeyStroke, getActionMap, getActionMap, getAlignmentX, getAlignmentY, getAncestorListeners, getAutoscrolls, getBorder, getBounds, getClientProperty, getComponentGraphics, getConditionForKeyStroke, getCreatedDoubleBuffer, getDebugGraphicsOptions, getDefaultLocale, getGraphics, getHeight, getInputMap, getInputMap, getInputMap, getInputVerifier, getInsets, getInsets, getListeners, getLocation, getManagingFocusBackwardTraversalKeys, getManagingFocusForwardTraversalKeys, getMaximumSize, getMinimumSize, getNextFocusableComponent, getPreferredSize, getPropertyChangeListeners, getPropertyChangeListeners, getRegisteredKeyStrokes, getRootPane, getSize, getToolTipLocation, getToolTipText, getToolTipText, getTopLevelAncestor, getTransferHandler, getVerifyInputWhenFocusTarget, getVetoableChangeListeners, getVisibleRect, getWidth, getWriteObjCounter, getX, getY, grabFocus, isDoubleBuffered, isLightweightComponent, isManagingFocus, isMaximumSizeSet, isMinimumSizeSet, isOpaque, isOptimizedDrawingEnabled, isPaintingTile, isPreferredSizeSet, isRequestFocusEnabled, isValidateRoot, paint, paintBorder, paintChildren, paintComponent, paintImmediately, paintImmediately, print, printAll, printBorder, printChildren, printComponent, processComponentKeyEvent, processKeyBinding, processKeyBindings, processKeyBindingsForAllComponents, processKeyEvent, processMouseMotionEvent, putClientProperty, rectangleIsObscured, registerKeyboardAction, registerKeyboardAction, removeAncestorListener, removeNotify, removePropertyChangeListener, removePropertyChangeListener, removeVetoableChangeListener, repaint, repaint, requestDefaultFocus, requestFocus, requestFocus, requestFocusInWindow, requestFocusInWindow, resetKeyboardActions, reshape, revalidate, scrollRectToVisible, setActionMap, setAlignmentX, setAlignmentY, setAutoscrolls, setBackground, setBorder, setCreatedDoubleBuffer, setDebugGraphicsOptions, setDefaultLocale, setDoubleBuffered, setEnabled, setFont, setForeground, setInputMap, setInputVerifier, setMaximumSize, setMinimumSize, setNextFocusableComponent, setOpaque, setPaintingChild, setPreferredSize, setRequestFocusEnabled, setToolTipText, setTransferHandler, setUI, setVerifyInputWhenFocusTarget, setVisible, setWriteObjCounter, shouldDebugGraphics, superProcessMouseMotionEvent, unregisterKeyboardAction, update
 
Methods inherited from class java.awt.Container
add, add, add, add, add, addContainerListener, addImpl, applyComponentOrientation, areFocusTraversalKeysSet, countComponents, deliverEvent, doLayout, findComponentAt, findComponentAt, get, getComponent, getComponentAt, getComponentAt, getComponentCount, getComponents, getContainerListeners, getFocusTraversalKeys, getFocusTraversalPolicy, getLayout, insets, invalidate, isAncestorOf, isFocusCycleRoot, isFocusCycleRoot, isFocusTraversalPolicySet, iterate, layout, list, list, locate, minimumSize, paintComponents, preferredSize, printComponents, processContainerEvent, processEvent, remove, remove, removeAll, removeContainerListener, setFocusCycleRoot, setFocusTraversalKeys, setFocusTraversalPolicy, setLayout, size, 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, getBackground, getBounds, getColorModel, getComponentListeners, getComponentOrientation, getCursor, getDropTarget, getFocusCycleRootAncestor, getFocusListeners, getFocusTraversalKeysEnabled, getFont, getFontMetrics, getForeground, getGraphicsConfiguration, getHierarchyBoundsListeners, getHierarchyListeners, getIgnoreRepaint, getInputContext, getInputMethodListeners, getInputMethodRequests, getKeyListeners, getLocale, getLocation, getLocationOnScreen, getMouseListeners, getMouseMotionListeners, getMouseWheelListeners, getName, getParent, getPeer, getSize, getToolkit, getTreeLock, gotFocus, handleEvent, hasFocus, hide, imageUpdate, inside, isBackgroundSet, isCursorSet, isDisplayable, isEnabled, isFocusable, isFocusOwner, isFocusTraversable, isFontSet, isForegroundSet, isLightweight, 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, processMouseEvent, processMouseWheelEvent, remove, removeComponentListener, removeFocusListener, removeHierarchyBoundsListener, removeHierarchyListener, removeInputMethodListener, removeKeyListener, removeMouseListener, removeMouseMotionListener, removeMouseWheelListener, repaint, repaint, repaint, resize, resize, setBounds, setBounds, setComponentOrientation, setCursor, setDropTarget, setFocusable, setFocusTraversalKeysEnabled, setIgnoreRepaint, setLocale, setLocation, setLocation, setName, setSize, setSize, show, show, toString, transferFocus, transferFocus, transferFocusUpCycle
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

uiClassID

private static final String uiClassID
See Also:
getUIClassID(), JComponent.readObject(java.io.ObjectInputStream)

selectionModel

private javax.swing.colorchooser.ColorSelectionModel selectionModel

previewPanel

private JComponent previewPanel

chooserPanels

private javax.swing.colorchooser.AbstractColorChooserPanel[] chooserPanels

dragEnabled

private boolean dragEnabled

SELECTION_MODEL_PROPERTY

public static final String SELECTION_MODEL_PROPERTY
Suppressed: The selection model property name.


PREVIEW_PANEL_PROPERTY

public static final String PREVIEW_PANEL_PROPERTY
Suppressed: The preview panel property name.


CHOOSER_PANELS_PROPERTY

public static final String CHOOSER_PANELS_PROPERTY
Suppressed: The chooserPanel array property name.


accessibleContext

protected AccessibleContext accessibleContext
Constructor Detail

JColorChooser

public JColorChooser()
Enabled: Creates a color chooser pane with an initial color of white.


JColorChooser

public JColorChooser(Color initialColor)
Enabled: Creates a color chooser pane with the specified initial color.

Parameters:
initialColor - the initial color set in the chooser

JColorChooser

public JColorChooser(javax.swing.colorchooser.ColorSelectionModel model)
Enabled: Creates a color chooser pane with the specified ColorSelectionModel.

Parameters:
model - the ColorSelectionModel to be used
Method Detail

showDialog

public static Color showDialog(Component component,
                               String title,
                               Color initialColor)
                        throws HeadlessException
Enabled: Shows a modal color-chooser dialog and blocks until the dialog is hidden. If the user presses the "OK" button, then this method hides/disposes the dialog and returns the selected color. If the user presses the "Cancel" button or closes the dialog without pressing "OK", then this method hides/disposes the dialog and returns null.

Parameters:
component - the parent Component for the dialog
title - the String containing the dialog's title
initialColor - the initial Color set when the color-chooser is shown
Returns:
the selected color or null if the user opted out
HeadlessException
See Also:
java.awt.GraphicsEnvironment#isHeadless

createDialog

public static JDialog createDialog(Component c,
                                   String title,
                                   boolean modal,
                                   JColorChooser chooserPane,
                                   ActionListener okListener,
                                   ActionListener cancelListener)
                            throws HeadlessException
Enabled: Creates and returns a new dialog containing the specified ColorChooser pane along with "OK", "Cancel", and "Reset" buttons. If the "OK" or "Cancel" buttons are pressed, the dialog is automatically hidden (but not disposed). If the "Reset" button is pressed, the color-chooser's color will be reset to the color which was set the last time show was invoked on the dialog and the dialog will remain showing.

Parameters:
c - the parent component for the dialog
title - the title for the dialog
modal - a boolean. When true, the remainder of the program is inactive until the dialog is closed.
chooserPane - the color-chooser to be placed inside the dialog
okListener - the ActionListener invoked when "OK" is pressed
cancelListener - the ActionListener invoked when "Cancel" is pressed
Returns:
a new dialog containing the color-chooser pane
HeadlessException
See Also:
java.awt.GraphicsEnvironment#isHeadless

getUI

public javax.swing.plaf.ColorChooserUI getUI()
Suppressed: Returns the L&F object that renders this component.

Returns:
the ColorChooserUI object that renders this component

setUI

public void setUI(javax.swing.plaf.ColorChooserUI ui)
Suppressed: Sets the L&F object that renders this component.

Parameters:
ui - the ColorChooserUI L&F object
See Also:
UIDefaults.getUI(javax.swing.JComponent)

updateUI

public void updateUI()
Suppressed: Notification from the UIManager that the L&F has changed. Replaces the current UI object with the latest version from the UIManager.

Overrides:
updateUI in class JComponent
See Also:
JComponent.updateUI()

getUIClassID

public String getUIClassID()
Suppressed: Returns the name of the L&F class that renders this component.

Overrides:
getUIClassID in class JComponent
Returns:
the string "ColorChooserUI"
See Also:
JComponent.getUIClassID(), UIDefaults.getUI(javax.swing.JComponent)

getColor

public Color getColor()
Enabled: Gets the current color value from the color chooser. By default, this delegates to the model.

Returns:
the current color value of the color chooser

setColor

public void setColor(Color color)
Enabled: Sets the current color of the color chooser to the specified color. The ColorSelectionModel will fire a ChangeEvent

Parameters:
color - the color to be set in the color chooser
See Also:
JComponent.addPropertyChangeListener(java.beans.PropertyChangeListener)

setColor

public void setColor(int r,
                     int g,
                     int b)
Enabled: Sets the current color of the color chooser to the specified RGB color. Note that the values of red, green, and blue should be between the numbers 0 and 255, inclusive.

Parameters:
r - an int specifying the amount of Red
g - an int specifying the amount of Green
b - an int specifying the amount of Blue
See Also:
java.awt.Color

setColor

public void setColor(int c)
Enabled: Sets the current color of the color chooser to the specified color.

Parameters:
c - an integer value that sets the current color in the chooser where the low-order 8 bits specify the Blue value, the next 8 bits specify the Green value, and the 8 bits above that specify the Red value.

setDragEnabled

public void setDragEnabled(boolean b)
Enabled: Sets the dragEnabled property, which must be true to enable automatic drag handling (the first part of drag and drop) on this component. The transferHandler property needs to be set to a non-null value for the drag to do anything. The default value of the dragEnabled property is false.

When automatic drag handling is enabled, most look and feels begin a drag-and-drop operation when the user presses the mouse button over the preview panel. Some look and feels might not support automatic drag and drop; they will ignore this property. You can work around such look and feels by modifying the component to directly call the exportAsDrag method of a TransferHandler.

Parameters:
b - the value to set the dragEnabled property to
Since:
1.4
See Also:
java.awt.GraphicsEnvironment#isHeadless, getDragEnabled(), JComponent.setTransferHandler(javax.swing.TransferHandler), TransferHandler

getDragEnabled

public boolean getDragEnabled()
Enabled: Gets the value of the dragEnabled property.

Returns:
the value of the dragEnabled property
Since:
1.4
See Also:
setDragEnabled(boolean)

setPreviewPanel

public void setPreviewPanel(JComponent preview)
Enabled: Sets the current preview panel. This will fire a PropertyChangeEvent for the property named "previewPanel".

Parameters:
preview - the JComponent which displays the current color
See Also:
JComponent.addPropertyChangeListener(java.beans.PropertyChangeListener)

getPreviewPanel

public JComponent getPreviewPanel()
Enabled: Returns the preview panel that shows a chosen color.

Returns:
a JComponent object -- the preview panel

addChooserPanel

public void addChooserPanel(javax.swing.colorchooser.AbstractColorChooserPanel panel)
Enabled: Adds a color chooser panel to the color chooser.

Parameters:
panel - the AbstractColorChooserPanel to be added

removeChooserPanel

public javax.swing.colorchooser.AbstractColorChooserPanel removeChooserPanel(javax.swing.colorchooser.AbstractColorChooserPanel panel)
Enabled: Removes the Color Panel specified.

Returns:
the color panel

setChooserPanels

public void setChooserPanels(javax.swing.colorchooser.AbstractColorChooserPanel[] panels)
Enabled: Specifies the Color Panels used to choose a color value.

Parameters:
panels - an array of AbstractColorChooserPanel objects

getChooserPanels

public javax.swing.colorchooser.AbstractColorChooserPanel[] getChooserPanels()
Enabled: Returns the specified color panels.

Returns:
an array of AbstractColorChooserPanel objects

getSelectionModel

public javax.swing.colorchooser.ColorSelectionModel getSelectionModel()
Enabled: Returns the data model that handles color selections.

Returns:
a ColorSelectionModel object

setSelectionModel

public void setSelectionModel(javax.swing.colorchooser.ColorSelectionModel newModel)
Enabled: Sets the model containing the selected color.

Parameters:
newModel - the new ColorSelectionModel object

writeObject

private void writeObject(ObjectOutputStream s)
                  throws IOException
See readObject and writeObject in JComponent for more information about serialization in Swing.

Parameters:
s - the ObjectOutputStream in which to write
IOException

paramString

protected String paramString()
Returns a string representation of this JColorChooser. This method is intended to be used only for debugging purposes, and the content and format of the returned string may vary between implementations. The returned string may be empty but may not be null.

Overrides:
paramString in class JComponent
Returns:
a string representation of this JColorChooser

getAccessibleContext

public AccessibleContext getAccessibleContext()
Suppressed: Gets the AccessibleContext associated with this JColorChooser. For color choosers, the AccessibleContext takes the form of an AccessibleJColorChooser. A new AccessibleJColorChooser instance is created if necessary.

Specified by:
getAccessibleContext in interface Accessible
Overrides:
getAccessibleContext in class JComponent
Returns:
an AccessibleJColorChooser that serves as the AccessibleContext of this JColorChooser


comments?