Clever way to change fonts on the fly...??  
Author Message
Mark Thornton





PostPosted: 8/6/2003 3:31:00 AM Top

java-programmer, Clever way to change fonts on the fly...?? blah wrote:

> All the various Swing componets I use have their "font values" set to a
> global font that is discovered in a config file when my app loads up. Now
> if I change the value, my application needs to be restarted in order to get
> the new font.
>
> I know I can go around and manually set the new chosen font into each and
> every component... but I'm wondering if there's an easier way to handle all
> of this...
>
> Anyone face this or have any suggestions?
>
>

Swing gets its fonts via UIManager. Override the default values and then
follow the procedure for changing the Look And Feel.

 
sandipchitale





PostPosted: 8/7/2003 12:57:00 AM Top

java-programmer >> Clever way to change fonts on the fly...?? "blah" <email***@***.com> wrote in message news:<zfTXa.418$email***@***.com>...
> All the various Swing componets I use have their "font values" set to a
> global font that is discovered in a config file when my app loads up. Now
> if I change the value, my application needs to be restarted in order to get
> the new font.
>
> I know I can go around and manually set the new chosen font into each and
> every component... but I'm wondering if there's an easier way to handle all
> of this...
>
> Anyone face this or have any suggestions?

Something like this may work:

import java.awt.Font;
import java.awt.Frame;
import java.awt.Window;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;

public class SwingFontManager {
public static void changeDefaultFont(Font font) {
UIManager.put("Button.font",font);
UIManager.put("ToggleButton.font",font);
UIManager.put("RadioButton.font",font);
UIManager.put("CheckBox.font",font);
UIManager.put("ColorChooser.font",font);
UIManager.put("ComboBox.font",font);
UIManager.put("Label.font",font);
UIManager.put("List.font",font);
UIManager.put("MenuBar.font",font);
UIManager.put("MenuItem.font",font);
UIManager.put("RadioButtonMenuItem.font",font);
UIManager.put("CheckBoxMenuItem.font",font);
UIManager.put("Menu.font",font);
UIManager.put("PopupMenu.font",font);
UIManager.put("OptionPane.font",font);
UIManager.put("Panel.font",font);
UIManager.put("ProgressBar.font",font);
UIManager.put("ScrollPane.font",font);
UIManager.put("Viewport.font",font);
UIManager.put("TabbedPane.font",font);
UIManager.put("Table.font",font);
UIManager.put("TableHeader.font",font);
UIManager.put("TextField.font",font);
UIManager.put("PasswordField.font",font);
UIManager.put("TextArea.font",font);
UIManager.put("TextPane.font",font);
UIManager.put("EditorPane.font",font);
UIManager.put("TitledBorder.font",font);
UIManager.put("ToolBar.font",font);
UIManager.put("ToolTip.font",font);
UIManager.put("Tree.font",font);

Frame[] frames = Frame.getFrames();
for (int i = 0; i < frames.length; i++) {
SwingUtilities.updateComponentTreeUI(frames[i]);
// the following could be recursive
Window[] windows = frames[i].getOwnedWindows();
for (int j = 0; j < windows.length; j++) {
SwingUtilities.updateComponentTreeUI(windows[j]);
}
}
}
}