public class JColorChooser extends JComponentimplements 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:
ActionListeners can be specified to be invoked when the user presses one of the dialog buttons. 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 XMLEncoder.
| Modifier and Type | Class and Description |
|---|---|
protected class |
JColorChooser
This class implements accessibility support for the
JColorChooser class.
|
JComponent.AccessibleJComponent Container.AccessibleAWTContainer Component.AccessibleAWTComponent , Component.BaselineResizeBehavior , Component.BltBufferStrategy , Component.FlipBufferStrategy | Modifier and Type | Field and Description |
|---|---|
protected AccessibleContext |
accessibleContext
|
static String |
CHOOSER_PANELS_PROPERTY
The chooserPanel array property name.
|
static String |
PREVIEW_PANEL_PROPERTY
The preview panel property name.
|
static String |
SELECTION_MODEL_PROPERTY
The selection model property name.
|
listenerList, TOOL_TIP_TEXT_KEY, ui, UNDEFINED_CONDITION, WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, WHEN_FOCUSED, WHEN_IN_FOCUSED_WINDOWBOTTOM_ALIGNMENT, CENTER_ALIGNMENT, LEFT_ALIGNMENT, RIGHT_ALIGNMENT, TOP_ALIGNMENTABORT, ALLBITS, ERROR, FRAMEBITS, HEIGHT, PROPERTIES, SOMEBITS, WIDTH| Constructor and Description |
|---|
JColorChooser()
Creates a color chooser pane with an initial color of white.
|
JColorChooser(Color
Creates a color chooser pane with the specified initial color.
|
JColorChooser(ColorSelectionModel
Creates a color chooser pane with the specified
ColorSelectionModel.
|
| Modifier and Type | Method and Description |
|---|---|
void |
addChooserPanel(AbstractColorChooserPanel
Adds a color chooser panel to the color chooser.
|
static JDialog |
createDialog(Component
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.
|
protected String |
paramString()
Returns a string representation of this
JColorChooser.
|
AbstractColorChooserPanel |
removeChooserPanel(AbstractColorChooserPanel
Removes the Color Panel specified.
|
void |
setChooserPanels(AbstractColorChooserPanel
Specifies the Color Panels used to choose a color value.
|
void |
setColor(Color
Sets the current color of the color chooser to the specified color.
|
void |
setColor(int c)
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
Sets the current preview panel.
|
void |
setSelectionModel(ColorSelectionModel
Sets the model containing the selected color.
|
void |
setUI(ColorChooserUI
Sets the L&F object that renders this component.
|
static Color |
showDialog(Component
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.
|
addAncestorListener, addNotify, addVetoableChangeListener, computeVisibleRect, contains, createToolTip, disable, enable, firePropertyChange, 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, updateadd, 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, validateTreeaction, 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, 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, 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, resize, resize, setBounds, setBounds, setComponentOrientation, setCursor, setDropTarget, setFocusable, setFocusTraversalKeysEnabled, setIgnoreRepaint, setLocale, setLocation, setLocation, setName, setSize, setSize, show, show, size, toString, transferFocus, transferFocusBackward, transferFocusUpCyclepublic static final StringSELECTION_MODEL_PROPERTY
public static final StringPREVIEW_PANEL_PROPERTY
public static final StringCHOOSER_PANELS_PROPERTY
protected AccessibleContextaccessibleContext
public JColorChooser()
public JColorChooser(ColorinitialColor)
initialColor - the initial color set in the chooser
public JColorChooser(ColorSelectionModelmodel)
ColorSelectionModel.
model - the
ColorSelectionModel to be used
public static ColorshowDialog(Component component, String title, Color initialColor) throws HeadlessException
null.
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
null if the user opted out
HeadlessException - if GraphicsEnvironment.isHeadless() returns true.
GraphicsEnvironment.isHeadless()
public static JDialogcreateDialog(Component c, String title, boolean modal, JColorChooser chooserPane, ActionListener okListener, ActionListener cancelListener) throws HeadlessException
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.
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
HeadlessException - if GraphicsEnvironment.isHeadless() returns true.
GraphicsEnvironment.isHeadless()
public ColorChooserUIgetUI()
ColorChooserUI object that renders this component
public void setUI(ColorChooserUIui)
ui - the
ColorChooserUI L&F object
UIDefaults.getUI(javax.swing.JComponent)
public void updateUI()
UIManager that the L&F has changed. Replaces the current UI object with the latest version from the
UIManager.
updateUI in class
JComponent
JComponent.updateUI()
public StringgetUIClassID()
getUIClassID in class
JComponent
JComponent.getUIClassID() ,
UIDefaults.getUI(javax.swing.JComponent)
public ColorgetColor()
public void setColor(Colorcolor)
ColorSelectionModel will fire a
ChangeEvent
color - the color to be set in the color chooser
Container.addPropertyChangeListener(java.beans.PropertyChangeListener)
public void setColor(int r,
int g,
int b)
r - an int specifying the amount of Red
g - an int specifying the amount of Green
b - an int specifying the amount of Blue
IllegalArgumentException - if r,g,b values are out of range
Color
public void setColor(int c)
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 setDragEnabled(boolean b)
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.
b - the value to set the
dragEnabled property to
HeadlessException - if
b is
true and
GraphicsEnvironment.isHeadless() returns
true
GraphicsEnvironment.isHeadless() ,
getDragEnabled(),
JComponent.setTransferHandler(javax.swing.TransferHandler) ,
TransferHandler
public boolean getDragEnabled()
dragEnabled property.
dragEnabled property
setDragEnabled(boolean)
public void setPreviewPanel(JComponentpreview)
PropertyChangeEvent for the property named "previewPanel".
preview - the
JComponent which displays the current color
Container.addPropertyChangeListener(java.beans.PropertyChangeListener)
public JComponentgetPreviewPanel()
JComponent object -- the preview panel
public void addChooserPanel(AbstractColorChooserPanelpanel)
panel - the
AbstractColorChooserPanel to be added
public AbstractColorChooserPanelremoveChooserPanel(AbstractColorChooserPanel panel)
panel - a string that specifies the panel to be removed
IllegalArgumentException - if panel is not in list of known chooser panels
public void setChooserPanels(AbstractColorChooserPanel[] panels)
panels - an array of
AbstractColorChooserPanel objects
public AbstractColorChooserPanel[] getChooserPanels()
AbstractColorChooserPanel objects
public ColorSelectionModelgetSelectionModel()
ColorSelectionModel object
public void setSelectionModel(ColorSelectionModelnewModel)
newModel - the new
ColorSelectionModel object
protected StringparamString()
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.
paramString in class
JComponent
JColorChooser
public AccessibleContextgetAccessibleContext()
getAccessibleContext in interface
Accessible
getAccessibleContext in class
Component