
304
|
Chapter 8, Rendering
#60 Changing Fonts Throughout Your Application
HACK
}
});
frame.getContentPane( ).add(button);
frame.pack( );
frame.setVisible(true);
}
And that’s it! Now you can add full-screen color choosing to any compo-
nent without requiring native access at all. As an improvement, you could
make the preview actually show a magnified view of where the cursor is
instead of just the selected color.
H A C K
#60
Changing Fonts Throughout Your Application Hack #60
Get a quick font face-lift, without having to write a whole Look and Feel.
With no standards documents to obey and more flexible user expectations,
web designers get much more freedom with their fonts than Swing develop-
ers expect. They get to set font styles with CSS, while we’re expected to just
leave well enough alone. Sure, you can change fonts on a component-by-
component basis with
setFont( ), but it’s not like you can just say “from
now on, I want all
JLabels to use the Cheese Deluxe Demi-Bold font.” Well,
OK, you could create a subclass of
JLabel to set that font in its constructor,
but your change wouldn’t be picked up by any of
JLabel’s subclasses, like
the default renderers for list, table, and tree cells. Fortunately, there is a
much easier way than fighting with single inheritance.
Swing components get many of their defaults (e.g., fonts, icons, borders),
from a
Hashtable owned by the UIManager class. Actually, it is a