Java Reference
In-Depth Information
Figure 9-19.
JColorChooser pop-up sample
In support of this behavior, the
JColorChooser
class requires the help of several support
classes found in the
javax.swing.colorchooser
package. The data model for the
JColorChooser
is an implementation of the
ColorSelectionModel
interface. The
javax.swing.colorchooser
package provides the
DefaultColorSelectionModel
class as an implementation of the
ColorSelectionModel
interface. For the user interface, the
JColorChooser
class relies on the
ColorChooserComponentFactory
to create the default panels from which to choose a color.
These panels are specific subclasses of the
AbstractColorChooserPanel
class, and if you don't
like the default set, you can create your own.
By default, when multiple chooser panels are in a
JColorChooser
, each panel is shown on
a tab of a
JTabbedPane
. However, the
ColorChooserUI
can deal with multiple panels in any way
it desires.
Creating a JColorChooser
If you want to create a
JColorChooser
and place it in your own window, you use one of the
following three constructors for the
JColorChooser
class:
public JColorChooser()
JColorChooser colorChooser = new JColorChooser();
public JColorChooser(Color initialColor)
JColorChooser colorChooser =
new JColorChooser(aComponent.getBackground());
public JColorChooser(ColorSelectionModel model)
JColorChooser colorChooser = new JColorChooser(aColorSelectionModel);
By default, the initial color for the chooser is white. If you don't want white as the default,
you can provide the initial color as a
Color
object or
ColorSelectionModel
.