APIdia Launch Partner
Top Description Inners Fields Constructors Methods
javax.swing

public Class JColorChooser

Additional top-level classes in compilation unit: ColorChooserDialog, ColorTracker.

extends JComponent
implements Accessible
Class Inheritance
All Implemented Interfaces
javax.accessibility.Accessible
Annotations
@JavaBean
defaultProperty:UI
description:A component that supports selecting a Color.
@SwingContainer:false
@SuppressWarnings:serial
Imports
java.awt.BorderLayout, .Color, .Component, .Container, .Dialog, .FlowLayout, .Frame, .GraphicsEnvironment, .HeadlessException, .Window, java.awt.event.ActionEvent, .ActionListener, .ComponentAdapter, .ComponentEvent, .KeyEvent, .WindowAdapter, .WindowEvent, java.beans.BeanProperty, .JavaBean, java.io.IOException, .ObjectOutputStream, .Serial, .Serializable, java.util.Arrays, .Locale, javax.accessibility.Accessible, .AccessibleContext, .AccessibleRole, javax.swing.colorchooser.AbstractColorChooserPanel, .ColorChooserComponentFactory, .ColorSelectionModel, .DefaultColorSelectionModel, javax.swing.plaf.ColorChooserUI, sun.swing.SwingUtilities2

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 JavaBeans has been added to the java.beans package. Please see java.beans.XMLEncoder.

Authors
James Gosling, Amy Fowler, Steve Wilson
Since
1.2

Nested and Inner Type Summary

Modifier and TypeClass and Description
protected class
JColorChooser.AccessibleJColorChooser

This class implements accessibility support for the JColorChooser class.

Field Summary

Modifier and TypeField and Description
protected AccessibleContext
public static final String
CHOOSER_PANELS_PROPERTY

The chooserPanel array property name.

private AbstractColorChooserPanel[]
private boolean
public static final String
PREVIEW_PANEL_PROPERTY

The preview panel property name.

private JComponent
public static final String
SELECTION_MODEL_PROPERTY

The selection model property name.

private ColorSelectionModel
private static final String
Inherited from javax.swing.JComponent:
DEBUG_GRAPHICS_LOADEDfocusControllerivSourceComponentlistenerListpaintingChildTOOL_TIP_TEXT_KEYuiUNDEFINED_CONDITIONWHEN_ANCESTOR_OF_FOCUSED_COMPONENTWHEN_FOCUSEDWHEN_IN_FOCUSED_WINDOW

Constructor Summary

AccessConstructor and Description
public
JColorChooser()

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

public
JColorChooser(Color
the initial color set in the chooser
initialColor
)

Creates a color chooser pane with the specified initial color.

public
JColorChooser(ColorSelectionModel
the ColorSelectionModel to be used
model
)

Creates a color chooser pane with the specified ColorSelectionModel.

Method Summary

Modifier and TypeMethod and Description
public void
addChooserPanel(AbstractColorChooserPanel
the AbstractColorChooserPanel to be added
panel
)

Adds a color chooser panel to the color chooser.

public static JDialog

Returns:

a new dialog containing the color-chooser pane
createDialog
(Component
the parent component for the dialog
c
,
String
the title for the dialog
title
,
boolean
a boolean. When true, the remainder of the program is inactive until the dialog is closed.
modal
,
JColorChooser
the color-chooser to be placed inside the dialog
chooserPane
,
ActionListener
the ActionListener invoked when "OK" is pressed
okListener
,
ActionListener
the ActionListener invoked when "Cancel" is pressed
cancelListener
)

Creates and returns a new dialog containing the specified ColorChooser pane along with "OK", "Cancel", and "Reset" buttons.

public AccessibleContext

Returns:

an AccessibleJColorChooser that serves as the AccessibleContext of this JColorChooser
getAccessibleContext
()

Overrides java.awt.Component.getAccessibleContext.

Implements javax.accessibility.Accessible.getAccessibleContext.

Gets the AccessibleContext associated with this JColorChooser.

public AbstractColorChooserPanel[]

Returns:

an array of AbstractColorChooserPanel objects
getChooserPanels
()

Returns the specified color panels.

public Color

Returns:

the current color value of the color chooser
getColor
()

Gets the current color value from the color chooser.

public boolean

Returns:

the value of the dragEnabled property
getDragEnabled
()

Gets the value of the dragEnabled property.

public JComponent

Returns:

a JComponent object -- the preview panel
getPreviewPanel
()

Returns the preview panel that shows a chosen color.

public ColorSelectionModel

Returns:

a ColorSelectionModel object
getSelectionModel
()

Returns the data model that handles color selections.

public ColorChooserUI

Returns:

the ColorChooserUI object that renders this component
getUI
()

Overrides javax.swing.JComponent.getUI.

Returns the L&F object that renders this component.

public String

Returns:

the string "ColorChooserUI"
getUIClassID
()

Overrides javax.swing.JComponent.getUIClassID.

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

protected String

Returns:

a string representation of this JColorChooser
paramString
()

Overrides javax.swing.JComponent.paramString.

Returns a string representation of this JColorChooser.

public AbstractColorChooserPanel

Returns:

the color panel
removeChooserPanel
(AbstractColorChooserPanel
a string that specifies the panel to be removed
panel
)

Removes the Color Panel specified.

public void
setChooserPanels(AbstractColorChooserPanel[]
an array of AbstractColorChooserPanel objects
panels
)

Specifies the Color Panels used to choose a color value.

public void
setColor(Color
the color to be set in the color chooser
color
)

Sets the current color of the color chooser to the specified color.

public void
setColor(int
an int specifying the amount of Red
r
,
int
an int specifying the amount of Green
g
,
int
an int specifying the amount of Blue
b
)

Sets the current color of the color chooser to the specified RGB color.

public void
setColor(int
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.
c
)

Sets the current color of the color chooser to the specified color.

public void
setDragEnabled(boolean
the value to set the dragEnabled property to
b
)

Sets the dragEnabled property, which must be true to enable automatic drag handling (the first part of drag and drop) on this component.

public void
setPreviewPanel(JComponent
the JComponent which displays the current color
preview
)

Sets the current preview panel.

public void
setSelectionModel(ColorSelectionModel
the new ColorSelectionModel object
newModel
)

Sets the model containing the selected color.

public void
setUI(ColorChooserUI
the ColorChooserUI L&F object
ui
)

Sets the L&F object that renders this component.

public static Color

Returns:

the selected color or null if the user opted out
showDialog
(Component
the parent Component for the dialog
component
,
String
the String containing the dialog's title
title
,
Color
the initial Color set when the color-chooser is shown
initialColor
)

Shows a modal color-chooser dialog and blocks until the dialog is hidden.

public static Color

Returns:

the selected color or null if the user opted out
showDialog
(Component
the parent Component for the dialog
component
,
String
the String containing the dialog's title
title
,
Color
the initial Color set when the color-chooser is shown
initialColor
,
boolean
true if the transparency of a color can be selected
colorTransparencySelectionEnabled
)

Shows a modal color-chooser dialog and blocks until the dialog is hidden.

public void
updateUI()

Overrides javax.swing.JComponent.updateUI.

Notification from the UIManager that the L&F has changed.

private void
writeObject(ObjectOutputStream s)

Hides javax.swing.JComponent.writeObject.

See readObject and writeObject in JComponent for more information about serialization in Swing.

Inherited from javax.swing.JComponent:
_paintImmediatelyaddAncestorListeneraddNotifyaddVetoableChangeListeneralwaysOnTopcheckIfChildObscuredBySiblingclientPropertyChangedcomponentInputMapChangedcomputeVisibleRectcomputeVisibleRectcompWriteObjectNotifycontainscreateToolTipdisabledndDonedropLocationForPointenablefirePropertyChangefirePropertyChangefirePropertyChangefireVetoableChangegetActionForKeyStrokegetActionMapgetActionMapgetAlignmentXgetAlignmentYgetAncestorListenersgetAutoscrollsgetBaselinegetBaselineResizeBehaviorgetBordergetBoundsgetClientPropertygetComponentGraphicsgetComponentPopupMenugetConditionForKeyStrokegetCreatedDoubleBuffergetDebugGraphicsOptionsgetDefaultLocalegetFontMetricsgetGraphicsgetGraphicsInvokedgetHeightgetInheritsPopupMenugetInputMapgetInputMapgetInputMapgetInputVerifiergetInsetsgetInsetsgetListenersgetLocationgetManagingFocusBackwardTraversalKeysgetManagingFocusForwardTraversalKeysgetMaximumSizegetMinimumSizegetNextFocusableComponentgetPopupLocationgetPreferredSizegetRegisteredKeyStrokesgetRootPanegetSizegetToolTipLocationgetToolTipTextgetToolTipTextgetTopLevelAncestorgetTransferHandlergetVerifyInputWhenFocusTargetgetVetoableChangeListenersgetVisibleRectgetWidthgetWriteObjCountergetXgetYgrabFocushideisDoubleBufferedisLightweightComponentisManagingFocusisOpaqueisOptimizedDrawingEnabledisPaintingisPaintingForPrintisPaintingOriginisPaintingTileisRequestFocusEnabledisValidateRootpaintpaintBorderpaintChildrenpaintComponentpaintForceDoubleBufferedpaintImmediatelypaintImmediatelypaintToOffscreenprintprintAllprintBorderprintChildrenprintComponentprocessComponentKeyEventprocessKeyBindingprocessKeyBindingsprocessKeyBindingsForAllComponentsprocessKeyEventprocessMouseEventprocessMouseMotionEventputClientPropertyrectangleIsObscuredregisterKeyboardActionregisterKeyboardActionremoveAncestorListenerremoveNotifyremoveVetoableChangeListenerrepaintrepaintrequestDefaultFocusrequestFocusrequestFocusrequestFocusInWindowrequestFocusInWindowresetKeyboardActionsreshaperevalidatesafelyGetGraphicssafelyGetGraphicsscrollRectToVisiblesetActionMapsetAlignmentXsetAlignmentYsetAutoscrollssetBackgroundsetBordersetComponentPopupMenusetCreatedDoubleBuffersetDebugGraphicsOptionssetDefaultLocalesetDoubleBufferedsetDropLocationsetEnabledsetFocusTraversalKeyssetFontsetForegroundsetInheritsPopupMenusetInputMapsetInputVerifiersetMaximumSizesetMinimumSizesetNextFocusableComponentsetOpaquesetPaintingChildsetPreferredSizesetRequestFocusEnabledsetToolTipTextsetTransferHandlersetUIsetUIPropertysetVerifyInputWhenFocusTargetsetVisiblesetWriteObjCountershouldDebugGraphicssuperProcessMouseMotionEventunregisterKeyboardActionupdate

Field Detail

accessibleContextback to summary
protected AccessibleContext accessibleContext

Hides java.awt.Component.accessibleContext.

The accessible context.

CHOOSER_PANELS_PROPERTYback to summary
public static final String CHOOSER_PANELS_PROPERTY

The chooserPanel array property name.

chooserPanelsback to summary
private AbstractColorChooserPanel[] chooserPanels
dragEnabledback to summary
private boolean dragEnabled
PREVIEW_PANEL_PROPERTYback to summary
public static final String PREVIEW_PANEL_PROPERTY

The preview panel property name.

previewPanelback to summary
private JComponent previewPanel
SELECTION_MODEL_PROPERTYback to summary
public static final String SELECTION_MODEL_PROPERTY

The selection model property name.

selectionModelback to summary
private ColorSelectionModel selectionModel
uiClassIDback to summary
private static final String uiClassID

Hides javax.swing.JComponent.uiClassID.

See Also
getUIClassID, readObject

Constructor Detail

JColorChooserback to summary
public JColorChooser()

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

JColorChooserback to summary
public JColorChooser(Color initialColor)

Creates a color chooser pane with the specified initial color.

Parameters
initialColor:Color

the initial color set in the chooser

JColorChooserback to summary
public JColorChooser(ColorSelectionModel model)

Creates a color chooser pane with the specified ColorSelectionModel.

Parameters
model:ColorSelectionModel

the ColorSelectionModel to be used

Method Detail

addChooserPanelback to summary
public void addChooserPanel(AbstractColorChooserPanel panel)

Adds a color chooser panel to the color chooser.

Parameters
panel:AbstractColorChooserPanel

the AbstractColorChooserPanel to be added

createDialogback to summary
public static JDialog createDialog(Component c, String title, boolean modal, JColorChooser chooserPane, ActionListener okListener, ActionListener cancelListener) throws HeadlessException

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:Component

the parent component for the dialog

title:String

the title for the dialog

modal:boolean

a boolean. When true, the remainder of the program is inactive until the dialog is closed.

chooserPane:JColorChooser

the color-chooser to be placed inside the dialog

okListener:ActionListener

the ActionListener invoked when "OK" is pressed

cancelListener:ActionListener

the ActionListener invoked when "Cancel" is pressed

Returns:JDialog

a new dialog containing the color-chooser pane

Exceptions
HeadlessException:
if GraphicsEnvironment.isHeadless() returns true.
See Also
java.awt.GraphicsEnvironment#isHeadless
getAccessibleContextback to summary
public AccessibleContext getAccessibleContext()

Overrides java.awt.Component.getAccessibleContext.

Implements javax.accessibility.Accessible.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:AccessibleContext

an AccessibleJColorChooser that serves as the AccessibleContext of this JColorChooser

Annotations
@BeanProperty
bound:false
getChooserPanelsback to summary
public AbstractColorChooserPanel[] getChooserPanels()

Returns the specified color panels.

Returns:AbstractColorChooserPanel[]

an array of AbstractColorChooserPanel objects

getColorback to summary
public Color getColor()

Gets the current color value from the color chooser. By default, this delegates to the model.

Returns:Color

the current color value of the color chooser

getDragEnabledback to summary
public boolean getDragEnabled()

Gets the value of the dragEnabled property.

Returns:boolean

the value of the dragEnabled property

Since
1.4
See Also
setDragEnabled
getPreviewPanelback to summary
public JComponent getPreviewPanel()

Returns the preview panel that shows a chosen color.

Returns:JComponent

a JComponent object -- the preview panel

getSelectionModelback to summary
public ColorSelectionModel getSelectionModel()

Returns the data model that handles color selections.

Returns:ColorSelectionModel

a ColorSelectionModel object

getUIback to summary
public ColorChooserUI getUI()

Overrides javax.swing.JComponent.getUI.

Returns the L&F object that renders this component.

Returns:ColorChooserUI

the ColorChooserUI object that renders this component

getUIClassIDback to summary
public String getUIClassID()

Overrides javax.swing.JComponent.getUIClassID.

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

Returns:String

the string "ColorChooserUI"

Annotations
@BeanProperty
bound:false
See Also
JComponent#getUIClassID, UIDefaults#getUI
paramStringback to summary
protected String paramString()

Overrides javax.swing.JComponent.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:String

a string representation of this JColorChooser

removeChooserPanelback to summary
public AbstractColorChooserPanel removeChooserPanel(AbstractColorChooserPanel panel)

Removes the Color Panel specified.

Parameters
panel:AbstractColorChooserPanel

a string that specifies the panel to be removed

Returns:AbstractColorChooserPanel

the color panel

Exceptions
IllegalArgumentException:
if panel is not in list of known chooser panels
setChooserPanelsback to summary
public void setChooserPanels(AbstractColorChooserPanel[] panels)

Specifies the Color Panels used to choose a color value.

Parameters
panels:AbstractColorChooserPanel[]

an array of AbstractColorChooserPanel objects

Annotations
@BeanProperty
hidden:true
description:An array of different chooser types.
setColorback to summary
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:Color

the color to be set in the color chooser

Annotations
@BeanProperty
bound:false
description:The current color the chooser is to display.
See Also
JComponent#addPropertyChangeListener
setColorback to summary
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:int

an int specifying the amount of Red

g:int

an int specifying the amount of Green

b:int

an int specifying the amount of Blue

Exceptions
IllegalArgumentException:
if r,g,b values are out of range
See Also
java.awt.Color
setColorback to summary
public void setColor(int c)

Sets the current color of the color chooser to the specified color.

Parameters
c:int

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.

setDragEnabledback to summary
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:boolean

the value to set the dragEnabled property to

Annotations
@BeanProperty
bound:false
description:Determines whether automatic drag handling is enabled.
Exceptions
HeadlessException:
if b is true and GraphicsEnvironment.isHeadless() returns true
Since
1.4
See Also
java.awt.GraphicsEnvironment#isHeadless, getDragEnabled, setTransferHandler, TransferHandler
setPreviewPanelback to summary
public void setPreviewPanel(JComponent preview)

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

Parameters
preview:JComponent

the JComponent which displays the current color

Annotations
@BeanProperty
hidden:true
description:The UI component which displays the current color.
See Also
JComponent#addPropertyChangeListener
setSelectionModelback to summary
public void setSelectionModel(ColorSelectionModel newModel)

Sets the model containing the selected color.

Parameters
newModel:ColorSelectionModel

the new ColorSelectionModel object

Annotations
@BeanProperty
hidden:true
description:The model which contains the currently selected color.
setUIback to summary
public void setUI(ColorChooserUI ui)

Sets the L&F object that renders this component.

Parameters
ui:ColorChooserUI

the ColorChooserUI L&F object

Annotations
@BeanProperty
hidden:true
description:The UI object that implements the color chooser\'s LookAndFeel.
See Also
UIDefaults#getUI
showDialogback to summary
public static Color showDialog(Component component, String title, Color initialColor) throws HeadlessException

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:Component

the parent Component for the dialog

title:String

the String containing the dialog's title

initialColor:Color

the initial Color set when the color-chooser is shown

Returns:Color

the selected color or null if the user opted out

Exceptions
HeadlessException:
if GraphicsEnvironment.isHeadless() returns true.
See Also
java.awt.GraphicsEnvironment#isHeadless
showDialogback to summary
public static Color showDialog(Component component, String title, Color initialColor, boolean colorTransparencySelectionEnabled) throws HeadlessException

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:Component

the parent Component for the dialog

title:String

the String containing the dialog's title

initialColor:Color

the initial Color set when the color-chooser is shown

colorTransparencySelectionEnabled:boolean

true if the transparency of a color can be selected

Returns:Color

the selected color or null if the user opted out

Annotations
@SuppressWarnings:deprecation
Exceptions
HeadlessException:
if GraphicsEnvironment.isHeadless() returns true.
Since
9
See Also
java.awt.GraphicsEnvironment#isHeadless
updateUIback to summary
public void updateUI()

Overrides javax.swing.JComponent.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
JComponent#updateUI
writeObjectback to summary
private void writeObject(ObjectOutputStream s) throws IOException

Hides javax.swing.JComponent.writeObject.

See readObject and writeObject in JComponent for more information about serialization in Swing.

Annotations
@Serial
Exceptions
IOException:

Doc from java.awt.Container.writeObject.

if an I/O error occurs

javax.swing back to summary

protected Class JColorChooser.AccessibleJColorChooser

extends AccessibleJComponent
Class Inheritance

This class implements accessibility support for the JColorChooser class. It provides an implementation of the Java Accessibility API appropriate to color chooser user-interface elements.

Field Summary

Inherited from javax.swing.JComponent.AccessibleJComponent:
accessibleFocusHandler

Constructor Summary

AccessConstructor and Description
protected
AccessibleJColorChooser()

Constructs an AccessibleJColorChooser.

Method Summary

Modifier and TypeMethod and Description
public AccessibleRole

Returns:

an instance of AccessibleRole describing the role of the object
getAccessibleRole
()

Overrides javax.swing.JComponent.AccessibleJComponent.getAccessibleRole.

Get the role of this object.

Inherited from javax.swing.JComponent.AccessibleJComponent:
addPropertyChangeListenergetAccessibleChildgetAccessibleChildrenCountgetAccessibleDescriptiongetAccessibleExtendedComponentgetAccessibleKeyBindinggetAccessibleNamegetAccessibleStateSetgetBorderTitlegetTitledBorderTextgetToolTipTextremovePropertyChangeListener

Constructor Detail

AccessibleJColorChooserback to summary
protected AccessibleJColorChooser()

Constructs an AccessibleJColorChooser.

Method Detail

getAccessibleRoleback to summary
public AccessibleRole getAccessibleRole()

Overrides javax.swing.JComponent.AccessibleJComponent.getAccessibleRole.

Get the role of this object.

Returns:AccessibleRole

an instance of AccessibleRole describing the role of the object

See Also
AccessibleRole