Solved

How to set Font size in Java application during run time

Posted on 2003-11-13
8
1,171 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 

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 250 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
jboss 7.1 start up error 1 83
Text effect for logo 17 64
Read CLOB data from Oracle using JAVA 3 40
Eclipse neon2 "Java build path" correctness 7 43
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
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.
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Suggested Courses

734 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