
8
|
Chapter 1, Basic JComponents
#2 Don’t Settle for Boring Text Labels
HACK
With the addition of this code to ImageTest’s main( ) method, the image-
based showcase program is complete. Figure 1-7 shows what the running
program looks like in the selected but disabled state.
H A C K
#2
Don’t Settle for Boring Text Labels Hack #2
JLabel is a Swing staple; but it’s easy to spruce up boring labels with drop
shadows, outlines, and even 3D text.
When you want to draw non-editable text, Swing provides only the JLabel.
You can change the font, size, color, and even add an icon. By using HTML
in your components
[Hack #52], you can even add things like underline and
bullets. This is fine for most jobs, but sometimes you need more. What if
you want a drop shadow or an embossed effect? The
JLabel is simply inade-
quate for richer interfaces. Fortunately, the Swing Team made it very easy to
extend the
JLabel and add these features yourself.
A great many text effects can be achieved with two simple features. First,
you can draw text multiple times, with each iteration slightly offset or in a
different color, to create effects like drop shadows and embossing. Second,
you can adjust the spacing between letters in a word (a feature known as
tracking in text-processing circles). Tracking is always specified in addition
to the default tracking specified by a font. Thus, a tracking of +1 would be
drawn as one extra pixel between ...