- All Implemented Interfaces:
- ImageObserver,- MenuContainer,- Serializable,- Accessible
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:
- A static convenience method which shows a modal color-chooser dialog and returns the color selected by the user.
- A static convenience method for creating a color-chooser dialog
 where ActionListenerscan be specified to be invoked when the user presses one of the dialog buttons.
- The ability to create instances of JColorChooserpanes directly (within any container).PropertyChangelisteners 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 XMLEncoder.
- Since:
- 1.2
- 
Nested Class SummaryNested ClassesModifier and TypeClassDescriptionprotected classThis class implements accessibility support for theJColorChooserclass.Nested classes/interfaces declared in class javax.swing.JComponentJComponent.AccessibleJComponentNested classes/interfaces declared in class java.awt.ContainerContainer.AccessibleAWTContainerNested classes/interfaces declared in class java.awt.ComponentComponent.AccessibleAWTComponent, Component.BaselineResizeBehavior, Component.BltBufferStrategy, Component.FlipBufferStrategy
- 
Field SummaryFieldsModifier and TypeFieldDescriptionprotected AccessibleContextThe accessible context.static final StringThe chooserPanel array property name.static final StringThe preview panel property name.static final StringThe selection model property name.Fields declared in class javax.swing.JComponentlistenerList, TOOL_TIP_TEXT_KEY, ui, UNDEFINED_CONDITION, WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, WHEN_FOCUSED, WHEN_IN_FOCUSED_WINDOWFields declared in class java.awt.ComponentBOTTOM_ALIGNMENT, CENTER_ALIGNMENT, LEFT_ALIGNMENT, RIGHT_ALIGNMENT, TOP_ALIGNMENTFields declared in interface java.awt.image.ImageObserverABORT, ALLBITS, ERROR, FRAMEBITS, HEIGHT, PROPERTIES, SOMEBITS, WIDTH
- 
Constructor SummaryConstructorsConstructorDescriptionCreates 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 specifiedColorSelectionModel.
- 
Method SummaryModifier and TypeMethodDescriptionvoidAdds a color chooser panel to the color chooser.static JDialogcreateDialog(Component c, String title, boolean modal, JColorChooser chooserPane, ActionListener okListener, ActionListener cancelListener) Creates and returns a new dialog containing the specifiedColorChooserpane along with "OK", "Cancel", and "Reset" buttons.Gets the AccessibleContext associated with this JColorChooser.Returns the specified color panels.getColor()Gets the current color value from the color chooser.booleanGets the value of thedragEnabledproperty.Returns the preview panel that shows a chosen color.Returns the data model that handles color selections.getUI()Returns the L&F object that renders this component.Returns the name of the L&F class that renders this component.protected StringReturns a string representation of thisJColorChooser.Removes the Color Panel specified.voidsetChooserPanels(AbstractColorChooserPanel[] panels) Specifies the Color Panels used to choose a color value.voidsetColor(int c) Sets the current color of the color chooser to the specified color.voidsetColor(int r, int g, int b) Sets the current color of the color chooser to the specified RGB color.voidSets the current color of the color chooser to the specified color.voidsetDragEnabled(boolean b) Sets thedragEnabledproperty, which must betrueto enable automatic drag handling (the first part of drag and drop) on this component.voidsetPreviewPanel(JComponent preview) Sets the current preview panel.voidsetSelectionModel(ColorSelectionModel newModel) Sets the model containing the selected color.voidsetUI(ColorChooserUI ui) Sets the L&F object that renders this component.static ColorshowDialog(Component component, String title, Color initialColor) Shows a modal color-chooser dialog and blocks until the dialog is hidden.static ColorshowDialog(Component component, String title, Color initialColor, boolean colorTransparencySelectionEnabled) Shows a modal color-chooser dialog and blocks until the dialog is hidden.voidupdateUI()Notification from theUIManagerthat the L&F has changed.Methods declared in class javax.swing.JComponentaddAncestorListener, addNotify, addVetoableChangeListener, computeVisibleRect, contains, createToolTip, disable, enable, 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, hide, isDoubleBuffered, isLightweightComponent, isManagingFocus, isOpaque, isOptimizedDrawingEnabled, isPaintingForPrint, isPaintingOrigin, isPaintingTile, isRequestFocusEnabled, isValidateRoot, paint, paintBorder, paintChildren, paintComponent, paintImmediately, paintImmediately, print, printAll, printBorder, printChildren, printComponent, processComponentKeyEvent, processKeyBinding, processKeyEvent, 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, setEnabled, setFocusTraversalKeys, setFont, setForeground, setInheritsPopupMenu, setInputMap, setInputVerifier, setMaximumSize, setMinimumSize, setNextFocusableComponent, setOpaque, setPreferredSize, setRequestFocusEnabled, setToolTipText, setTransferHandler, setUI, setVerifyInputWhenFocusTarget, setVisible, unregisterKeyboardAction, updateMethods declared in class java.awt.Containeradd, 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, transferFocusDownCycle, validate, validateTreeMethods declared in class java.awt.Componentaction, 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, 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, getPropertyChangeListeners, getPropertyChangeListeners, getSize, getToolkit, getTreeLock, gotFocus, handleEvent, hasFocus, 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, requestFocus, requestFocus, requestFocusInWindow, resize, resize, setBounds, setBounds, setComponentOrientation, setCursor, setDropTarget, setFocusable, setFocusTraversalKeysEnabled, setIgnoreRepaint, setLocale, setLocation, setLocation, setMixingCutoutShape, setName, setSize, setSize, show, show, size, toString, transferFocus, transferFocusBackward, transferFocusUpCycle
- 
Field Details- 
SELECTION_MODEL_PROPERTYThe selection model property name.- See Also:
 
- 
PREVIEW_PANEL_PROPERTYThe preview panel property name.- See Also:
 
- 
CHOOSER_PANELS_PROPERTYThe chooserPanel array property name.- See Also:
 
- 
accessibleContextThe accessible context.
 
- 
- 
Constructor Details- 
JColorChooserpublic JColorChooser()Creates a color chooser pane with an initial color of white.
- 
JColorChooserCreates a color chooser pane with the specified initial color.- Parameters:
- initialColor- the initial color set in the chooser
 
- 
JColorChooserCreates a color chooser pane with the specifiedColorSelectionModel.- Parameters:
- model- the- ColorSelectionModelto be used
 
 
- 
- 
Method Details- 
showDialogpublic 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 returnsnull.- Parameters:
- component- the parent- Componentfor 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 nullif the user opted out
- Throws:
- HeadlessException- if GraphicsEnvironment.isHeadless() returns true.
- See Also:
 
- 
showDialogpublic 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 returnsnull.- Parameters:
- component- the parent- Componentfor the dialog
- title- the String containing the dialog's title
- initialColor- the initial Color set when the color-chooser is shown
- colorTransparencySelectionEnabled- true if the transparency of a color can be selected
- Returns:
- the selected color or nullif the user opted out
- Throws:
- HeadlessException- if GraphicsEnvironment.isHeadless() returns true.
- Since:
- 9
- See Also:
 
- 
createDialogpublic 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 specifiedColorChooserpane 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 timeshowwas 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:
 
- 
getUIReturns the L&F object that renders this component.- Overrides:
- getUIin class- JComponent
- Returns:
- the ColorChooserUIobject that renders this component
 
- 
setUI@BeanProperty(hidden=true, description="The UI object that implements the color chooser\'s LookAndFeel.") public void setUI(ColorChooserUI ui) Sets the L&F object that renders this component.- Parameters:
- ui- the- ColorChooserUIL&F object
- See Also:
 
- 
updateUIpublic void updateUI()Notification from theUIManagerthat the L&F has changed. Replaces the current UI object with the latest version from theUIManager.- Overrides:
- updateUIin class- JComponent
- See Also:
 
- 
getUIClassIDReturns the name of the L&F class that renders this component.- Overrides:
- getUIClassIDin class- JComponent
- Returns:
- the string "ColorChooserUI"
- See Also:
 
- 
getColorGets 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@BeanProperty(bound=false, description="The current color the chooser is to display.") public void setColor(Color color) Sets the current color of the color chooser to the specified color. TheColorSelectionModelwill fire aChangeEvent- Parameters:
- color- the color to be set in the color chooser
- See Also:
 
- 
setColorpublic 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:
 
- 
setColorpublic 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.
 
- 
setDragEnabled@BeanProperty(bound=false, description="Determines whether automatic drag handling is enabled.") public void setDragEnabled(boolean b) Sets thedragEnabledproperty, which must betrueto enable automatic drag handling (the first part of drag and drop) on this component. ThetransferHandlerproperty needs to be set to a non-nullvalue for the drag to do anything. The default value of thedragEnabledproperty isfalse.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 exportAsDragmethod of aTransferHandler.- Parameters:
- b- the value to set the- dragEnabledproperty to
- Throws:
- HeadlessException- if- bis- trueand- GraphicsEnvironment.isHeadless()returns- true
- Since:
- 1.4
- See Also:
 
- 
getDragEnabledpublic boolean getDragEnabled()Gets the value of thedragEnabledproperty.- Returns:
- the value of the dragEnabledproperty
- Since:
- 1.4
- See Also:
 
- 
setPreviewPanel@BeanProperty(hidden=true, description="The UI component which displays the current color.") public void setPreviewPanel(JComponent preview) Sets the current preview panel. This will fire aPropertyChangeEventfor the property named "previewPanel".- Parameters:
- preview- the- JComponentwhich displays the current color
- See Also:
 
- 
getPreviewPanelReturns the preview panel that shows a chosen color.- Returns:
- a JComponentobject -- the preview panel
 
- 
addChooserPanelAdds a color chooser panel to the color chooser.- Parameters:
- panel- the- AbstractColorChooserPanelto be added
 
- 
removeChooserPanelRemoves 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
 
- 
setChooserPanels@BeanProperty(hidden=true, description="An array of different chooser types.") public void setChooserPanels(AbstractColorChooserPanel[] panels) Specifies the Color Panels used to choose a color value.- Parameters:
- panels- an array of- AbstractColorChooserPanelobjects
 
- 
getChooserPanelsReturns the specified color panels.- Returns:
- an array of AbstractColorChooserPanelobjects
 
- 
getSelectionModelReturns the data model that handles color selections.- Returns:
- a ColorSelectionModelobject
 
- 
setSelectionModel@BeanProperty(hidden=true, description="The model which contains the currently selected color.") public void setSelectionModel(ColorSelectionModel newModel) Sets the model containing the selected color.- Parameters:
- newModel- the new- ColorSelectionModelobject
 
- 
paramStringReturns a string representation of thisJColorChooser. 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 benull.- Overrides:
- paramStringin class- JComponent
- Returns:
- a string representation of this JColorChooser
 
- 
getAccessibleContextGets 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:
- getAccessibleContextin interface- Accessible
- Overrides:
- getAccessibleContextin class- Component
- Returns:
- an AccessibleJColorChooser that serves as the AccessibleContext of this JColorChooser
 
 
-