Java Reference
In-Depth Information
a title-bordered container, and having the foreground of the preview panel change when the
user selects a new color.
■
Caution
A bug in the
ColorChooserUI
implementation class (
BasicColorChooserUI
) requires an
extra step to properly install the preview panel. Besides calling
setPreviewPanel(newPanel)
, you must
set the panel's size and border to enable the user interface to properly configure the new preview panel.
The exact steps seem to vary with which JDK release you are using. See
http://bugs.sun.com/
bugdatabase/view_bug.do?bug_id=5029286
for more details. There are some other related bugs
(search the Bug Parade for
setPreviewPanel
).
The following source demonstrates the use of a
JLabel
as the custom preview panel with
the necessary work-around. Figure 9-21 demonstrates what the
JColorChooser
that uses this
preview panel would look like.
final JLabel previewLabel = new JLabel("I Love Swing", JLabel.CENTER);
previewLabel.setFont(new Font("Serif", Font.BOLD | Font.ITALIC, 48));
previewLabel.setSize(previewLabel.getPreferredSize());
previewLabel.setBorder(BorderFactory.createEmptyBorder(0,0,1,0));
colorChooser.setPreviewPanel(previewLabel);
Figure 9-21.
JColorChooser with custom preview panel
■
Note
Because the initial setting for the foreground of the preview panel is its background color, the panel
will appear to be empty. This is one reason why the default preview panel shows text with contrasting back-
ground colors.