Solved

How to set Font size in Java application during run time

Posted on 2003-11-13
8
1,106 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
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

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.

 

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
Application launch issue with Apache Tomcat 5 64
difference of if loops 23 62
use lov values 2 63
ejb example issues 3 28
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
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 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…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

830 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