
Load New Fonts at Runtime #61
Chapter 8, Rendering
|
307
HACK
These are all JComponents, but they’re notably lacking the “J”
that starts the name of typical Swing components (
Tree.font
for JTree, TextArea.font for JTextArea, etc.).
Now that you know the keys for the UIDefaults table, instead of blindly
changing all of them to the same font, you can customize a look by applying
different fonts—or different sizes and styles of one or two base fonts—to
different widgets.
H A C K
#61
Load New Fonts at Runtime Hack #61
Who cares what fonts your users have? Bundle the fonts you want your
application to use and load those fonts dynamically!
Using fonts with any predictability used to be a nightmare in Java. For a
while, you could only depend on having access to one serif, one sans-serif,
and one monospaced font, and the constants you’d use to get those fonts
changed between Java 1.0 and 1.1. Fortunately, you’re now free to use any
font installed on the user’s machine and load it by name.
Of course, not everyone has the same fonts. Even different installations of
the same operating system will have different fonts available. I still use some
TrueType fonts I’ve been toting from machine to machine for 15 years, and
it’s a safe bet that very few other people will have those same fonts.
This would seem to limit your Swing application to using only the fonts you
know are installed with an operating system—maybe Arial ...