O'Reilly logo

Swing Hacks by Chris Adamson, Joshua Marinacci

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 subclass of
Hashtable, called UIDefaults, which offers strongly typed methods like
getFont( ), getBorder( ), getColor( ), etc., each of which takes a key object.
Now, since this is just a
Hashtable, you can put stuff in just as easily as you
can get it out. All you have to do is know what the key is. As it turns out, for
fonts, the keys are
Strings that end with a .font suffix. So, for demonstra-
tion purposes, you can iterate through the keys of the
UIDefaults, and every
time you find one that ends in
.font, put the Font of your choice back into
the
UIDefaults.
Changing the Default Fonts
The goal of the ChangeAllFonts example is to change the default font of all
Swing components, by changing all the appropriate keys it can find in
UIDefaults. It starts by getting a font name from the command line and cre-
ating a 12-point plain
Font instance.
Changing Fonts Throughout Your Application #60
Chapter 8, Rendering
|
305
HACK
Next, it gets the UIDefaults object as a Hashtable and gets an Enumeration of
its keys. It walks the enumeration and, for every key ending in
.font, it uses
put( )
to replace the previous font with the user-selected font.
Finally, it creates a simple GUI with several typical
JComponent
s and shows
them in a
JFrame
. This short example is shown in Example 8-7.
Example 8-7. Changing default fonts via UIDefaults
public class ChangeAllFonts {
final static String[] LIST_ITEMS =
{ "JList", "with", "new Font" };
public static void main (String[] args) {
try {
// get user's font
if (args.length < 1) {
System.out.println (
"Usage: ChangeAllFonts font-name");
return;
}
String fontName = args[0];
Font font = new Font (fontName, Font.PLAIN, 12);
// put this font in the defaults table for every
// ui font resource key
Hashtable defaults = UIManager.getDefaults( );
Enumeration keys = defaults.keys( );
while (keys.hasMoreElements( )) {
Object key = keys.nextElement( );
if ((key instanceof String) &&
(((String) key).endsWith(".font"))) {
System.out.println (key);
defaults.put (key, font);
}
}
// now bring up a GUI to show this off
JPanel panel = new JPanel( );
panel.setLayout (new BoxLayout (panel, BoxLayout.Y_AXIS));
panel.add (new JLabel ("JLabel with font " + fontName));
panel.add (new JTextField ("JTextField with font " +
fontName));
panel.add (new JButton ("JButton with font " +
fontName));
JList list = new JList (LIST_ITEMS);
JScrollPane pane =
new JScrollPane (list,
ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,
ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
panel.add (pane);

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required