
26
|
Chapter 1, Basic JComponents
#6 Watermark Your Scroll Panes
HACK
H A C K
#6
Watermark Your Scroll Panes
Hack #6
This hack creates a text area with a tiled background image that is fixed,
even when the text area scrolls, and also a fixed foreground image that
appears above the text, much like the station badges now affixed to the
lower-righthand corner of most TV broadcasts.
The Swing framework was designed to let developers override portions of
every component, both the visual appearance (the view) and the behavior
(the model and controller). This design gives developers great flexibility.
One of my favorites is the
JScrollPane. Its nested composite design allows
developers to create some stunning effects.
Once again, the idea is to override the drawing code of a standard compo-
nent to create the visual effects
[Hack #5]. The difference here is that you must
deal with a composite object, the
JScrollPane. A JScrollPane is not a single
Swing component—it’s actually a wrapper around two scrollbars and the
component that does the real scrolling is a
JViewport. This viewport is the
actual target component; you will subclass it to draw both above and below
the
View component (as seen in Example 1-12). The View is the Swing wid-
get being scrolled; in this case, it is a
JTextArea.
The
ScrollPaneWatermark class inherits from JViewport, adding two meth-
ods:
setBackgroundTexture( ) and setForegroundBadge( ). Each ...