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
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
Fo global setting a defaults You can use:
ex:
UIDefaults defaults = UIManager.getDefaults();
defaults.put("Label.font", aFont);
defaults.put("List.font", aFont1);
....
ex:
UIDefaults defaults = UIManager.getDefaults();
defaults.put("Label.font",
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(Action Listener al);
public static void removeSettingsListener(Act ionListene r al);
}
class ULabel extends JLabel
{
public ULabel()
{
super();
SettingsServer.addSettings Listener(n ew ActionListener()
{
public void actionPerformed(ActionEven t e)
{
setFont(SettingsServer.get DefaultFon t);
}
});
}
}
This is an template of proposed solution. It's needed to implement the rest of methods.
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(Action
public static void removeSettingsListener(Act
}
class ULabel extends JLabel
{
public ULabel()
{
super();
SettingsServer.addSettings
{
public void actionPerformed(ActionEven
{
setFont(SettingsServer.get
}
});
}
}
This is an template of proposed solution. It's needed to implement the rest of methods.
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
defaults.put("Label.font",
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!
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.nextE lement().t oString()) ;
}
I suppose that changing those values at runtime will act only on newly created components - components already existing won't be updated.
UIDefaults d = UIManager.getDefaults();
Enumeration e = d.keys();
while (e.hasMoreElements())
{
System.out.println(e.nextE
}
I suppose that changing those values at runtime will act only on newly created components - components already existing won't be updated.
ASKER
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
It is setting the font for newly created components.
Thanx
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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!