2

How can I change the font of all components that are displayed in my java application?

I tried it using the UIManager

UIManager.put("TextField.font",  new java.awt.Font("Arial Unicode MS", java.awt.Font.PLAIN, 11));
UIManager.put("Label.font",  new java.awt.Font("Arial Unicode MS", java.awt.Font.PLAIN, 11));
UIManager.put("ComboBox.font",  new java.awt.Font("Arial Unicode MS", java.awt.Font.PLAIN, 11))

Oddly enough this changed the font of the textfield but did not work for JLabels or JComboBoxes

Then I tried to set the font by looping over all keys the UIManager knows:

public static void setUIFont(FontUIResource f) {
    Enumeration keys = UIManager.getDefaults().keys();
    while (keys.hasMoreElements()) {
        Object key = keys.nextElement();
        Object value = UIManager.get(key);
        if (value instanceof FontUIResource) {
            FontUIResource orig = (FontUIResource) value;
            Font font = new Font(f.getFontName(), orig.getStyle(), f.getSize());
            FontUIResource fontUIResource = new FontUIResource(font);
            UIManager.put(key, fontUIResource);
            UIManager.getDefaults().put(key, fontUIResource);
            UIManager.getLookAndFeel().getDefaults().put(key, fontUIResource);
        }
    }
}

This code did not work at all.

I must say that I use Synthetica as LookAndFeel... so I don't know how that interferes with my manuell settings via UIManager.

Stephen Kennedy
  • 20,585
  • 22
  • 95
  • 108
mareicha
  • 21
  • 3
  • 1
    [Moreover it enables you to modify existing Themes and to create your own look and feel only by editing a XML-based configuration file - you don't have to write complex Java-GUI-Code.](http://www.javasoft.de/synthetica/) – mKorbel May 27 '15 at 12:19
  • voting tgo close as off-topics – mKorbel May 27 '15 at 12:21

2 Answers2

0

I think you can update this answer from Roman as follows:

public static void setUIFont (java.awt.Font f){
    java.util.Enumeration keys = UIManager.getDefaults().keys();
    while (keys.hasMoreElements()) {
      Object key = keys.nextElement();
      Object value = UIManager.get (key);
      if (value != null && value instanceof java.awt.Font)
        UIManager.put (key, f);
      }
}

Then use it with your Font like this:

setUIFont (new java.awt.Font("Arial Unicode MS", java.awt.Font.PLAIN, 11));
Community
  • 1
  • 1
Jordi Castilla
  • 26,609
  • 8
  • 70
  • 109
0

You can create your own components like:

public class MyJButton extends JButton {

    public MyJButton(){
        setFont(FontClass.getBasicFont());
    }

}

public class MyJTextArea extends JTextArea {

    public MyJTextArea (){
        setFont(FontClass.getBasicFont());
    }

}
.
.
.

And then create this class which holds all your fonts properties like:

public class FontClass(){

    private static final String BASIC_FONT_FACE = "Arial";

    private static final String BASIC_FONT_SIZE = 12;

    public static Font getBasicFont(){
        return new Font(BASIC_FONT_FACE, Font.PLAIN, BASIC_FONT_SIZE);
    }

}

I suppose that this is the primitive way, but it can be good if you want to save these values in your DB

a_z
  • 342
  • 1
  • 3
  • 14