Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

How to set Font size in Java application during run time

Posted on 2003-11-13
8
Medium Priority
?
1,297 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
ID: 9745674
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
ID: 9745760
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
ID: 9745804
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:pavan_k_goyal
ID: 9752596
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
 
LVL 4

Expert Comment

by:vk33
ID: 9757946
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
ID: 9762251
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
ID: 9854801
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 750 total points
ID: 9855717
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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Suggested Courses

926 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