Link to home
Start Free TrialLog in
Avatar of pavan_k_goyal
pavan_k_goyal

asked on

How to set Font size in Java application during run time

I have one java application. I have written that application by setting default font size=10.
Now I want to set the default font during run time i.e. I will choose font size from one option and font size of all components(all components on currently opened screens/menu etc and for any window that will be opened after setting font size) should be set to choosen font size.

Thanx
Pavan
Avatar of vk33
vk33

Hi!

Use a global font object. Something like this:

JFrame createMyDialog(Font font) {
   JLabel label = new JLabel("hello");
   label.setFont(font);
   ...
}

So, when constructing GUI you can set assign the font to all the dialogs:

Font italic = new Font("Dialog", Font.BOLD, 10);
JFrame dialog = createMyDialog(italic);
...

Hope this helps!
Fo global setting a defaults You can use:
ex:

UIDefaults defaults = UIManager.getDefaults();
defaults.put("Label.font", aFont);
defaults.put("List.font", aFont1);
....

For changing at runtime You need to do it manually or create components with this functionality - some kind of settings server.
Each component should on particular event refresh settings from the server.
ex:
class SettingsServer
{
    private static Arraylist settingsListeners = new ArrayList();
    private static Font defaultFont = new Font(....);
    public static void setDefaultFont(Font font)
    {
         defaultFont = font;
         fireSettingsChanged();
    }
    public static Font getDefaultFont()
    {
         return defaultFont;
    }
    public static void fireSettingsChanged()
   {
       //call all registered listeners
   }
   //eed to add listeners managing
   public static void addSettingsListener(ActionListener al);
   public static void removeSettingsListener(ActionListener al);

}
class ULabel extends JLabel
{
    public ULabel()
    {
        super();
        SettingsServer.addSettingsListener(new ActionListener()
        {
              public void actionPerformed(ActionEvent e)
              {
                   setFont(SettingsServer.getDefaultFont);
              }
        });
    }
}

This is an template of proposed solution. It's needed to implement the rest of methods.
Avatar of pavan_k_goyal

ASKER

UIDefaults defaults = UIManager.getDefaults();
defaults.put("Label.font", aFont);
defaults.put("List.font", aFont1);
......

I'm using Java swing. Can you give me the list of All components that

like for Label : Label.font

similarly  for JTable ?? what ..... for JButton ???

would it be JTable.font ??? or some thing else
setFont() is a method of JComponent. Thus you can use it for all JComponent subclasses (most of the Swing controls). For a complete list see the javadoc (direct known subclasses and futher on):

http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/JComponent.html

Regards!
This code lists all properties in UIDefaults:

UIDefaults d = UIManager.getDefaults();
Enumeration e = d.keys();
while (e.hasMoreElements())
{
    System.out.println(e.nextElement().toString());
}

I suppose that changing those values at runtime will act only on newly created components - components already existing won't be updated.
Thanx it worked . But problem is how to reset the font of existing(visible)  components.
It is setting the font for newly created components.

Thanx
ASKER CERTIFIED SOLUTION
Avatar of vk33
vk33

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial