FileDocCategorySizeDatePackage
JColorChooser.javaAPI DocJava SE 5 API24436Fri Aug 26 14:57:54 BST 2005javax.swing

JColorChooser

public class JColorChooser extends JComponent implements 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:

  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 {@link java.beans.XMLEncoder}.

beaninfo
attribute: isContainer false description: A component that supports selecting a Color.
version
1.47 12/19/03
author
James Gosling
author
Amy Fowler
author
Steve Wilson

Fields Summary
private static final String
uiClassID
private ColorSelectionModel
selectionModel
private JComponent
previewPanel
private AbstractColorChooserPanel[]
chooserPanels
private boolean
dragEnabled
public static final String
SELECTION_MODEL_PROPERTY
The selection model property name.
public static final String
PREVIEW_PANEL_PROPERTY
The preview panel property name.
public static final String
CHOOSER_PANELS_PROPERTY
The chooserPanel array property name.
protected AccessibleContext
accessibleContext
Constructors Summary
public JColorChooser()
Creates a color chooser pane with an initial color of white.

        this(Color.white);
    
public JColorChooser(Color initialColor)
Creates a color chooser pane with the specified initial color.

param
initialColor the initial color set in the chooser

	this( new DefaultColorSelectionModel(initialColor) );

    
public JColorChooser(ColorSelectionModel model)
Creates a color chooser pane with the specified ColorSelectionModel.

param
model the ColorSelectionModel to be used

	selectionModel = model;
        updateUI();
	dragEnabled = false;
    
Methods Summary
public voidaddChooserPanel(javax.swing.colorchooser.AbstractColorChooserPanel panel)
Adds a color chooser panel to the color chooser.

param
panel the AbstractColorChooserPanel to be added

        AbstractColorChooserPanel[] oldPanels = getChooserPanels();
        AbstractColorChooserPanel[] newPanels = new AbstractColorChooserPanel[oldPanels.length+1];
	System.arraycopy(oldPanels, 0, newPanels, 0, oldPanels.length);
	newPanels[newPanels.length-1] = panel;
	setChooserPanels(newPanels);
    
public static javax.swing.JDialogcreateDialog(java.awt.Component c, java.lang.String title, boolean modal, javax.swing.JColorChooser chooserPane, java.awt.event.ActionListener okListener, java.awt.event.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.

param
c the parent component for the dialog
param
title the title for the dialog
param
modal a boolean. When true, the remainder of the program is inactive until the dialog is closed.
param
chooserPane the color-chooser to be placed inside the dialog
param
okListener the ActionListener invoked when "OK" is pressed
param
cancelListener the ActionListener invoked when "Cancel" is pressed
return
a new dialog containing the color-chooser pane
exception
HeadlessException if GraphicsEnvironment.isHeadless() returns true.
see
java.awt.GraphicsEnvironment#isHeadless


        Window window = JOptionPane.getWindowForComponent(c);
	ColorChooserDialog dialog;
        if (window instanceof Frame) {
            dialog = new ColorChooserDialog((Frame)window, title, modal, c, chooserPane,
					    okListener, cancelListener);
        } else {
            dialog = new ColorChooserDialog((Dialog)window, title, modal, c, chooserPane,
					    okListener, cancelListener);
        }
        dialog.addWindowListener(new ColorChooserDialog.Closer());
        dialog.addComponentListener(new ColorChooserDialog.DisposeOnClose());
	return dialog;
    
public javax.accessibility.AccessibleContextgetAccessibleContext()
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.

return
an AccessibleJColorChooser that serves as the AccessibleContext of this JColorChooser


                                                          
       
        if (accessibleContext == null) {
            accessibleContext = new AccessibleJColorChooser();
        }
        return accessibleContext;
    
public javax.swing.colorchooser.AbstractColorChooserPanel[]getChooserPanels()
Returns the specified color panels.

return
an array of AbstractColorChooserPanel objects

        return chooserPanels;
    
public java.awt.ColorgetColor()
Gets the current color value from the color chooser. By default, this delegates to the model.

return
the current color value of the color chooser

        return selectionModel.getSelectedColor();
    
public booleangetDragEnabled()
Gets the value of the dragEnabled property.

return
the value of the dragEnabled property
see
#setDragEnabled
since
1.4

	return dragEnabled;
    
public javax.swing.JComponentgetPreviewPanel()
Returns the preview panel that shows a chosen color.

return
a JComponent object -- the preview panel

        return previewPanel;
    
public javax.swing.colorchooser.ColorSelectionModelgetSelectionModel()
Returns the data model that handles color selections.

return
a ColorSelectionModel object

        return selectionModel;
    
public javax.swing.plaf.ColorChooserUIgetUI()
Returns the L&F object that renders this component.

return
the ColorChooserUI object that renders this component

        return (ColorChooserUI)ui;
    
public java.lang.StringgetUIClassID()
Returns the name of the L&F class that renders this component.

return
the string "ColorChooserUI"
see
JComponent#getUIClassID
see
UIDefaults#getUI

        return uiClassID;
    
protected java.lang.StringparamString()
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.

return
a string representation of this JColorChooser

	StringBuffer chooserPanelsString = new StringBuffer("");
	for (int i=0; i<chooserPanels.length; i++) {
	    chooserPanelsString.append("[" + chooserPanels[i].toString()
				       + "]");
	}
        String previewPanelString = (previewPanel != null ?
				     previewPanel.toString() : "");

        return super.paramString() +
        ",chooserPanels=" + chooserPanelsString.toString() +
        ",previewPanel=" + previewPanelString;
    
public javax.swing.colorchooser.AbstractColorChooserPanelremoveChooserPanel(javax.swing.colorchooser.AbstractColorChooserPanel panel)
Removes the Color Panel specified.

param
panel a string that specifies the panel to be removed
return
the color panel
exception
IllegalArgumentException if panel is not in list of known chooser panels



	int containedAt = -1;

        for (int i = 0; i < chooserPanels.length; i++) {
  	    if (chooserPanels[i] == panel) {
	        containedAt = i;
		break;
	    }
	}
	if (containedAt == -1) {
	    throw new IllegalArgumentException("chooser panel not in this chooser");
	}

        AbstractColorChooserPanel[] newArray = new AbstractColorChooserPanel[chooserPanels.length-1];

	if (containedAt == chooserPanels.length-1) {  // at end
	    System.arraycopy(chooserPanels, 0, newArray, 0, newArray.length);
	}
	else if (containedAt == 0) {  // at start
	    System.arraycopy(chooserPanels, 1, newArray, 0, newArray.length);
	}
	else {  // in middle
	    System.arraycopy(chooserPanels, 0, newArray, 0, containedAt);
	    System.arraycopy(chooserPanels, containedAt+1,
			     newArray, containedAt, (chooserPanels.length - containedAt - 1));
	}

	setChooserPanels(newArray);

	return panel;
    
public voidsetChooserPanels(javax.swing.colorchooser.AbstractColorChooserPanel[] panels)
Specifies the Color Panels used to choose a color value.

param
panels an array of AbstractColorChooserPanel objects
beaninfo
bound: true hidden: true description: An array of different chooser types.

        AbstractColorChooserPanel[] oldValue = chooserPanels;
	chooserPanels = panels;
	firePropertyChange(CHOOSER_PANELS_PROPERTY, oldValue, panels);
    
public voidsetColor(java.awt.Color color)
Sets the current color of the color chooser to the specified color. The ColorSelectionModel will fire a ChangeEvent

param
color the color to be set in the color chooser
see
JComponent#addPropertyChangeListener
beaninfo
bound: false hidden: false description: The current color the chooser is to display.

        selectionModel.setSelectedColor(color);

    
public voidsetColor(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.

param
r an int specifying the amount of Red
param
g an int specifying the amount of Green
param
b an int specifying the amount of Blue
exception
IllegalArgumentException if r,g,b values are out of range
see
java.awt.Color

        setColor(new Color(r,g,b));
    
public voidsetColor(int c)
Sets the current color of the color chooser to the specified color.

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

        setColor((c >> 16) & 0xFF, (c >> 8) & 0xFF, c & 0xFF);
    
public voidsetDragEnabled(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.

param
b the value to set the dragEnabled property to
exception
HeadlessException if b is true and GraphicsEnvironment.isHeadless() returns true
since
1.4
see
java.awt.GraphicsEnvironment#isHeadless
see
#getDragEnabled
see
#setTransferHandler
see
TransferHandler
beaninfo
description: Determines whether automatic drag handling is enabled. bound: false

        if (b && GraphicsEnvironment.isHeadless()) {
            throw new HeadlessException();
        }
	dragEnabled = b;
    
public voidsetPreviewPanel(javax.swing.JComponent preview)
Sets the current preview panel. This will fire a PropertyChangeEvent for the property named "previewPanel".

param
preview the JComponent which displays the current color
see
JComponent#addPropertyChangeListener
beaninfo
bound: true hidden: true description: The UI component which displays the current color.


        if (previewPanel != preview) {
	    JComponent oldPreview = previewPanel;
	    previewPanel = preview;
            firePropertyChange(JColorChooser.PREVIEW_PANEL_PROPERTY, oldPreview, preview);
        }
    
public voidsetSelectionModel(javax.swing.colorchooser.ColorSelectionModel newModel)
Sets the model containing the selected color.

param
newModel the new ColorSelectionModel object
beaninfo
bound: true hidden: true description: The model which contains the currently selected color.

        ColorSelectionModel oldModel = selectionModel;
	selectionModel = newModel;
	firePropertyChange(JColorChooser.SELECTION_MODEL_PROPERTY, oldModel, newModel);
    
public voidsetUI(javax.swing.plaf.ColorChooserUI ui)
Sets the L&F object that renders this component.

param
ui the ColorChooserUI L&F object
see
UIDefaults#getUI
beaninfo
bound: true hidden: true description: The UI object that implements the color chooser's LookAndFeel.

        super.setUI(ui);
    
public static java.awt.ColorshowDialog(java.awt.Component component, java.lang.String title, java.awt.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.

param
component the parent Component for the dialog
param
title the String containing the dialog's title
param
initialColor the initial Color set when the color-chooser is shown
return
the selected color or null if the user opted out
exception
HeadlessException if GraphicsEnvironment.isHeadless() returns true.
see
java.awt.GraphicsEnvironment#isHeadless



                                                                                                                       
        
              

        final JColorChooser pane = new JColorChooser(initialColor != null?
                                               initialColor : Color.white);

        ColorTracker ok = new ColorTracker(pane);
        JDialog dialog = createDialog(component, title, true, pane, ok, null);

        dialog.show(); // blocks until user brings dialog down...

        return ok.getColor();
    
public voidupdateUI()
Notification from the UIManager that the L&F has changed. Replaces the current UI object with the latest version from the UIManager.

see
JComponent#updateUI

        setUI((ColorChooserUI)UIManager.getUI(this));
    
private voidwriteObject(java.io.ObjectOutputStream s)
See readObject and writeObject in JComponent for more information about serialization in Swing.

        s.defaultWriteObject();
        if (getUIClassID().equals(uiClassID)) {
            byte count = JComponent.getWriteObjCounter(this);
            JComponent.setWriteObjCounter(this, --count);
            if (count == 0 && ui != null) {
                ui.installUI(this);
            }
        }