Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting the Global Font for a Java Application

Tags:

java

fonts

swing

I need to set the the default font for my application. Is there a way to do this that is not LaF dependent?

like image 546
user489041 Avatar asked Apr 28 '11 20:04

user489041


People also ask

How do I change font size in Java application?

You can't actually change the size of an existing Font object. The best way to achieve a similar effect is to use the deriveFont(size) method to create a new almost identical Font that is a different size.

What is the default font in Java GUI?

It doesn't have a default font. It depends on what LookandFeel you are using.

What is set font in Java?

In Java, Font is a class that belongs to the java. awt package. It implements the Serializable interface. FontUIResource is the direct known subclass of the Java Font class. It represents the font that are used to render the text.


1 Answers

Figured it out:

Call with: setUIFont (new javax.swing.plaf.FontUIResource(new Font("MS Mincho",Font.PLAIN, 12)));

private static void setUIFont(javax.swing.plaf.FontUIResource f)
{
    java.util.Enumeration<Object> keys = UIManager.getDefaults().keys();
    while (keys.hasMoreElements())
    {
        Object key = keys.nextElement();
        Object value = UIManager.get(key);
        if (value instanceof javax.swing.plaf.FontUIResource)
        {
            UIManager.put(key, f);
        }
    }
}
like image 164
user489041 Avatar answered Oct 19 '22 11:10

user489041