Setting the Global Font for a Java Application
Asked Answered
T

2

8

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

Tanker answered 28/4, 2011 at 20:7 Comment(0)
T
6

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);
        }
    }
}
Tanker answered 28/4, 2011 at 20:19 Comment(2)
hmm .. what a strange requirement: are you sure you want the exact same font for everything? labels, textComponents, headers, borders, whatever? Users might be confused.Corycorybant
Yea, I need it to be the same font. Reason being is I need it to be a custom font we use to support special characters. Everything in the program is 12 point font, so size and all that shouldnt be an issue.Tanker
C
4

for better control about how/which fonts to replace - in a LAF independent way, but controllable per-laf - have a look at the JGoodies Looks project

http://java.net/projects/looks

It allows to swap entire FontSets (that's a collection of semantic fonts, like control, dialog, message) at runtime.

Corycorybant answered 29/4, 2011 at 11:37 Comment(1)
Awesome, ill have to check into thisTanker

© 2022 - 2024 — McMap. All rights reserved.