public class

JColorChooser

extends JComponent
implements Accessible
java.lang.Object
   ↳ java.awt.Component
     ↳ java.awt.Container
       ↳ javax.swing.JComponent
         ↳ javax.swing.JColorChooser

Class Overview

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: Swing is not thread safe. For more information see Swing's Threading Policy.

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 XMLEncoder.

Summary

Nested Classes
class JColorChooser.AccessibleJColorChooser This class implements accessibility support for the JColorChooser class. 
Constants
String CHOOSER_PANELS_PROPERTY The chooserPanel array property name.
String PREVIEW_PANEL_PROPERTY The preview panel property name.
String SELECTION_MODEL_PROPERTY The selection model property name.
[Expand]
Inherited Constants
From class javax.swing.JComponent
From class java.awt.Component
From interface java.awt.image.ImageObserver
Fields
protected AccessibleContext accessibleContext
[Expand]
Inherited Fields
From class javax.swing.JComponent
Public Constructors
JColorChooser()
Creates a color chooser pane with an initial color of white.
JColorChooser(Color initialColor)
Creates a color chooser pane with the specified initial color.
JColorChooser(ColorSelectionModel model)
Creates a color chooser pane with the specified ColorSelectionModel.
Public Methods
void addChooserPanel(AbstractColorChooserPanel panel)
Adds a color chooser panel to the color chooser.
static JDialog createDialog(Component c, String title, boolean modal, JColorChooser chooserPane, ActionListener okListener, ActionListener cancelListener)
Creates and returns a new dialog containing the specified ColorChooser pane along with "OK", "Cancel", and "Reset" buttons.
AccessibleContext getAccessibleContext()
Gets the AccessibleContext associated with this JColorChooser.
AbstractColorChooserPanel[] getChooserPanels()
Returns the specified color panels.
Color getColor()
Gets the current color value from the color chooser.
boolean getDragEnabled()
Gets the value of the dragEnabled property.
JComponent getPreviewPanel()
Returns the preview panel that shows a chosen color.
ColorSelectionModel getSelectionModel()
Returns the data model that handles color selections.
ColorChooserUI getUI()
Returns the L&F object that renders this component.
String getUIClassID()
Returns the name of the L&F class that renders this component.
AbstractColorChooserPanel removeChooserPanel(AbstractColorChooserPanel panel)
Removes the Color Panel specified.
void setChooserPanels(AbstractColorChooserPanel[] panels)
Specifies the Color Panels used to choose a color value.
void setColor(int c)
Sets the current color of the color chooser to the specified color.
void setColor(Color color)
Sets the current color of the color chooser to the specified color.
void setColor(int r, int g, int b)
Sets the current color of the color chooser to the specified RGB color.
void setDragEnabled(boolean b)
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)
Sets the current preview panel.
void setSelectionModel(ColorSelectionModel newModel)
Sets the model containing the selected color.
void setUI(ColorChooserUI ui)
Sets the L&F object that renders this component.
static Color showDialog(Component component, String title, Color initialColor)
Shows a modal color-chooser dialog and blocks until the dialog is hidden.
void updateUI()
Notification from the UIManager that the L&F has changed.
Protected Methods
String paramString()
Returns a string representation of this JColorChooser.
[Expand]
Inherited Methods
From class javax.swing.JComponent
From class java.awt.Container
From class java.awt.Component
From class java.lang.Object
From interface java.awt.MenuContainer
From interface java.awt.image.ImageObserver
From interface javax.accessibility.Accessible

Constants

public static final String CHOOSER_PANELS_PROPERTY

The chooserPanel array property name.

Constant Value: "chooserPanels"

public static final String PREVIEW_PANEL_PROPERTY

The preview panel property name.

Constant Value: "previewPanel"

public static final String SELECTION_MODEL_PROPERTY

The selection model property name.

Constant Value: "selectionModel"

Fields

protected AccessibleContext accessibleContext

Public Constructors

public JColorChooser ()

Creates a color chooser pane with an initial color of white.

public JColorChooser (Color initialColor)

Creates a color chooser pane with the specified initial color.

Parameters
initialColor the initial color set in the chooser

public JColorChooser (ColorSelectionModel model)

Creates a color chooser pane with the specified ColorSelectionModel.

Parameters
model the ColorSelectionModel to be used

Public Methods

public void addChooserPanel (AbstractColorChooserPanel panel)

Adds a color chooser panel to the color chooser.

Parameters
panel the AbstractColorChooserPanel to be added

public static JDialog createDialog (Component c, String title, boolean modal, JColorChooser chooserPane, ActionListener okListener, ActionListener cancelListener)

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
Throws
HeadlessException if GraphicsEnvironment.isHeadless() returns true.
See Also

public AccessibleContext getAccessibleContext ()

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.

Returns
  • an AccessibleJColorChooser that serves as the AccessibleContext of this JColorChooser

public AbstractColorChooserPanel[] getChooserPanels ()

Returns the specified color panels.

Returns
  • an array of AbstractColorChooserPanel objects

public Color getColor ()

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

public boolean getDragEnabled ()

Gets the value of the dragEnabled property.

Returns
  • the value of the dragEnabled property

public JComponent getPreviewPanel ()

Returns the preview panel that shows a chosen color.

Returns
  • a JComponent object -- the preview panel

public ColorSelectionModel getSelectionModel ()

Returns the data model that handles color selections.

Returns
  • a ColorSelectionModel object

public ColorChooserUI getUI ()

Returns the L&F object that renders this component.

Returns
  • the ColorChooserUI object that renders this component

public String getUIClassID ()

Returns the name of the L&F class that renders this component.

Returns
  • the string "ColorChooserUI"

public AbstractColorChooserPanel removeChooserPanel (AbstractColorChooserPanel panel)

Removes the Color Panel specified.

Parameters
panel a string that specifies the panel to be removed
Returns
  • the color panel
Throws
IllegalArgumentException if panel is not in list of known chooser panels

public void setChooserPanels (AbstractColorChooserPanel[] panels)

Specifies the Color Panels used to choose a color value.

Parameters
panels an array of AbstractColorChooserPanel objects

public void setColor (int c)

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.

public void setColor (Color color)

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

public void setColor (int r, int g, int b)

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
Throws
IllegalArgumentException if r,g,b values are out of range
See Also

public void setDragEnabled (boolean b)

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
Throws
HeadlessException if b is true and GraphicsEnvironment.isHeadless() returns true

public void setPreviewPanel (JComponent preview)

Sets the current preview panel. This will fire a PropertyChangeEvent for the property named "previewPanel".

Parameters
preview the JComponent which displays the current color

public void setSelectionModel (ColorSelectionModel newModel)

Sets the model containing the selected color.

Parameters
newModel the new ColorSelectionModel object

public void setUI (ColorChooserUI ui)

Sets the L&F object that renders this component.

Parameters
ui the ColorChooserUI L&F object

public static Color showDialog (Component component, String title, Color initialColor)

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
Throws
HeadlessException if GraphicsEnvironment.isHeadless() returns true.
See Also

public void updateUI ()

Notification from the UIManager that the L&F has changed. Replaces the current UI object with the latest version from the UIManager.

See Also

Protected Methods

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.

Returns
  • a string representation of this JColorChooser