8

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

user489041
  • 27,916
  • 55
  • 135
  • 204

2 Answers2

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);
        }
    }
}
EM-Creations
  • 4,195
  • 4
  • 40
  • 56
user489041
  • 27,916
  • 55
  • 135
  • 204
  • 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. – kleopatra Apr 29 '11 at 10:48
  • 1
    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. – user489041 Apr 29 '11 at 13:56
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.

kleopatra
  • 51,061
  • 28
  • 99
  • 211