
Instead of displaying a generic JColorChooser with the static showDialog()
method, you can create your own instance of the JColorChooser class. You can
then set properties on the color chooser object and display it in any way you
want. The static
JColorChooser.createDialog() method is useful here. It creates a
dialog box to hold your
JColorChooser pane and allows you to specify two
ActionListener objects that are invoked in response to the OK and Cancel but-
tons in the dialog box.
You can customize a
JColorChooser by adding a new color selection panel or a
new color preview panel. To add a new color selection panel (for example, a
panel that allows the user to select a grayscale color or a CMYK color), implement
a subclass of
AbstractColorChooserPanel (fr om the javax.swing.colorchooser
package) and pass it to the addChooserPanel() method of your JColorChooser.
Your custom panel contains a
ColorSelectionModel that serves as the interface
between your pane and the
JColorChooser. All your pane needs to do is update
the selected color of its
ColorSelectionModel (ColorSelectionModel is also part
of the
javax.swing.colorchooser package).
You can use any
JComponent as a custom preview panel for your JColorChooser.
Simply pass the component to
setPreviewPanel(). The preview component has to
track the currently selected color by listening for
ChangeEvent events generated by
the
ColorSelectionModel of the JColorChooser ...