Solved

How to set Font size in Java application during run time

Posted on 2003-11-13
8
1,046 Views
Last Modified: 2013-12-03
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
0
Comment
Question by:pavan_k_goyal
  • 3
  • 3
  • 2
8 Comments
 
LVL 4

Expert Comment

by:vk33
Comment Utility
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!
0
 
LVL 2

Expert Comment

by:Tols
Comment Utility
Fo global setting a defaults You can use:
ex:

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

0
 
LVL 2

Expert Comment

by:Tols
Comment Utility
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.
0
 

Author Comment

by:pavan_k_goyal
Comment Utility
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
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 4

Expert Comment

by:vk33
Comment Utility
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!
0
 
LVL 2

Expert Comment

by:Tols
Comment Utility
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.
0
 

Author Comment

by:pavan_k_goyal
Comment Utility
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
0
 
LVL 4

Accepted Solution

by:
vk33 earned 250 total points
Comment Utility
Create SettingsServer, as Tols suggested, and add events handlers to your containers as well. It's needed to repaint the components area. btw, revalidation is also necessary as the components might have changed their size.
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This video teaches viewers about errors in exception handling.

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now