73

I was wondering how to set the default font for my entire Java swing program. From my research it appears it can be done with UIManager, something to do with LookAndFeel, but I can't find specifically how to do it, and the UIManager appears pretty complicated.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Connor Neville
  • 1,028
  • 2
  • 9
  • 13
  • if you'll look at right site on this window, then please scroll down, there are I can see column `Related` with 3-5 excelent threads about similair issue – mKorbel Sep 15 '11 at 17:17

12 Answers12

72

try:

public static void setUIFont (javax.swing.plaf.FontUIResource f){
    java.util.Enumeration 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);
      }
    } 

Call by ...

setUIFont (new javax.swing.plaf.FontUIResource("Serif",Font.ITALIC,12));
Ryan Hilbert
  • 1,805
  • 1
  • 18
  • 31
Romain Hippeau
  • 24,113
  • 5
  • 60
  • 79
  • 3
    For some reason, this is only changing the font on the text of the tabs on my JTabbedPane. – Connor Neville Sep 15 '11 at 22:43
  • 2
    I had it working using `UIManager.getLookAndFeelDefaults()` instead of `UIManager.getDefaults()` and using the returned reference instead of `UIManager.put()` (see my answer below). It works only when overriding the default L&F though (Nimbus in my case)... – Matthieu Aug 21 '14 at 16:26
44
UIManager.put("Button.font", /* font of your liking */);
UIManager.put("ToggleButton.font", /* font of your liking */);
UIManager.put("RadioButton.font", /* font of your liking */);
UIManager.put("CheckBox.font", /* font of your liking */);
UIManager.put("ColorChooser.font", /* font of your liking */);
UIManager.put("ComboBox.font", /* font of your liking */);
UIManager.put("Label.font", /* font of your liking */);
UIManager.put("List.font", /* font of your liking */);
UIManager.put("MenuBar.font", /* font of your liking */);
UIManager.put("MenuItem.font", /* font of your liking */);
UIManager.put("RadioButtonMenuItem.font", /* font of your liking */);
UIManager.put("CheckBoxMenuItem.font", /* font of your liking */);
UIManager.put("Menu.font", /* font of your liking */);
UIManager.put("PopupMenu.font", /* font of your liking */);
UIManager.put("OptionPane.font", /* font of your liking */);
UIManager.put("Panel.font", /* font of your liking */);
UIManager.put("ProgressBar.font", /* font of your liking */);
UIManager.put("ScrollPane.font", /* font of your liking */);
UIManager.put("Viewport.font", /* font of your liking */);
UIManager.put("TabbedPane.font", /* font of your liking */);
UIManager.put("Table.font", /* font of your liking */);
UIManager.put("TableHeader.font", /* font of your liking */);
UIManager.put("TextField.font", /* font of your liking */);
UIManager.put("PasswordField.font", /* font of your liking */);
UIManager.put("TextArea.font", /* font of your liking */);
UIManager.put("TextPane.font", /* font of your liking */);
UIManager.put("EditorPane.font", /* font of your liking */);
UIManager.put("TitledBorder.font", /* font of your liking */);
UIManager.put("ToolBar.font", /* font of your liking */);
UIManager.put("ToolTip.font", /* font of your liking */);
UIManager.put("Tree.font", /* font of your liking */);

Source: http://www.jguru.com/faq/view.jsp?EID=340519

Amir Raminfar
  • 33,777
  • 7
  • 93
  • 123
  • waw... is that working by adding New Font object to the /* font of your liking */ portion? Bcoz, it seems that Once the JFRame created and running... There's no effect I obtained. Is there anything I Forgotten @Amir Raminfar? – gumuruh Dec 22 '11 at 10:20
  • 2
    Source's source: [BasicLookAndFeel.java](http://hg.openjdk.java.net/jdk7/jsn/jdk/file/tip/src/share/classes/javax/swing/plaf/basic/BasicLookAndFeel.java) – flup Aug 16 '13 at 15:02
  • 1
    Additional information: Some components have multiple keys to set the font for a distinct part of the component. For example JOptionPane: To set the default fonts for JOptionPane you should use: `UIManager.put("OptionPane.messageFont", /*font*/);` and `UIManager.put("OptionPane.buttonFont", /*font*/);` – matthiasboesinger Nov 14 '14 at 17:52
  • 1
    @gumuruh it works when you refresh the content tree `SwingUtilities.updateComponentTreeUI(this);` – Asce4s Feb 23 '17 at 10:22
22
java -Dswing.aatext=true -Dswing.plaf.metal.controlFont=Tahoma -Dswing.plaf.metal.userFont=Tahoma …

This will not only set Tahoma on your complete UI but also turn on anti-aliasing which makes any font much more beautiful immediately.

Bombe
  • 81,643
  • 20
  • 123
  • 127
13

I think this is better, calling it for the current laf instead of the whole UIManager put this

UIManager.getLookAndFeelDefaults()
        .put("defaultFont", new Font("Arial", Font.BOLD, 14));

Somewhere in the main before instantiating your JFrame object. It worked perfectly for me. Remember this is the default font, for the components that have no specified font.

source: http://www.java.net/node/680725

sherif
  • 163
  • 1
  • 4
6

Inspired by Romain Hippeau, use this code if you want to set just the font size.

for (Map.Entry<Object, Object> entry : javax.swing.UIManager.getDefaults().entrySet()) {
    Object key = entry.getKey();
    Object value = javax.swing.UIManager.get(key);
    if (value != null && value instanceof javax.swing.plaf.FontUIResource) {
        javax.swing.plaf.FontUIResource fr=(javax.swing.plaf.FontUIResource)value;
        javax.swing.plaf.FontUIResource f = new javax.swing.plaf.FontUIResource(fr.getFamily(), fr.getStyle(), FONT_SIZE);
        javax.swing.UIManager.put(key, f);
    }
}
Pavel Sedek
  • 519
  • 5
  • 13
5

Be aware that the way to set the default font depends on the Look And Feel you're using. The solution described by Romain Hippeau works fine with a lot of LAF but not with Nimbus. The one posted by sherif works fine with Nimbus, but not with others (Metal, for instance).

Combining both could work on most of LAF, but none of these solutions works with GTK+ LAF.

I think (but I'm not sure), there's no cross-platform solution.

Jean-Marc Astesana
  • 1,242
  • 3
  • 16
  • 24
3

As a completion of @Amir answer, this is the complete list of keys

I use this function

private void setFont(FontUIResource myFont) {
    UIManager.put("CheckBoxMenuItem.acceleratorFont", myFont);
    UIManager.put("Button.font", myFont);
    UIManager.put("ToggleButton.font", myFont);
    UIManager.put("RadioButton.font", myFont);
    UIManager.put("CheckBox.font", myFont);
    UIManager.put("ColorChooser.font", myFont);
    UIManager.put("ComboBox.font", myFont);
    UIManager.put("Label.font", myFont);
    UIManager.put("List.font", myFont);
    UIManager.put("MenuBar.font", myFont);
    UIManager.put("Menu.acceleratorFont", myFont);
    UIManager.put("RadioButtonMenuItem.acceleratorFont", myFont);
    UIManager.put("MenuItem.acceleratorFont", myFont);
    UIManager.put("MenuItem.font", myFont);
    UIManager.put("RadioButtonMenuItem.font", myFont);
    UIManager.put("CheckBoxMenuItem.font", myFont);
    UIManager.put("OptionPane.buttonFont", myFont);
    UIManager.put("OptionPane.messageFont", myFont);
    UIManager.put("Menu.font", myFont);
    UIManager.put("PopupMenu.font", myFont);
    UIManager.put("OptionPane.font", myFont);
    UIManager.put("Panel.font", myFont);
    UIManager.put("ProgressBar.font", myFont);
    UIManager.put("ScrollPane.font", myFont);
    UIManager.put("Viewport.font", myFont);
    UIManager.put("TabbedPane.font", myFont);
    UIManager.put("Slider.font", myFont);
    UIManager.put("Table.font", myFont);
    UIManager.put("TableHeader.font", myFont);
    UIManager.put("TextField.font", myFont);
    UIManager.put("Spinner.font", myFont);
    UIManager.put("PasswordField.font", myFont);
    UIManager.put("TextArea.font", myFont);
    UIManager.put("TextPane.font", myFont);
    UIManager.put("EditorPane.font", myFont);
    UIManager.put("TabbedPane.smallFont", myFont);
    UIManager.put("TitledBorder.font", myFont);
    UIManager.put("ToolBar.font", myFont);
    UIManager.put("ToolTip.font", myFont);
    UIManager.put("Tree.font", myFont);
    UIManager.put("FormattedTextField.font", myFont);
    UIManager.put("IconButton.font", myFont);
    UIManager.put("InternalFrame.optionDialogTitleFont", myFont);
    UIManager.put("InternalFrame.paletteTitleFont", myFont);
    UIManager.put("InternalFrame.titleFont", myFont);
}

and i call it in main before invoking the ui

setFont(new FontUIResource(new Font("Cabin", Font.PLAIN, 14)));

For a complete list of Swing UI Manager keys check this link

moolsbytheway
  • 1,152
  • 13
  • 20
  • 1
    note calling it BEFORE invoking the ui is necessary else only few places the font effect takes place. – yolob 21 Sep 24 '20 at 08:15
1

The correct answer is the one given by Amir Raminfar but you have to encapsulate the font as a FontUIResource.

For example:

UIManager.put("Button.font", new FontUIResource(new Font ("Helvetica", Font.BOLD, 16)));
Adriano
  • 3,788
  • 5
  • 32
  • 53
0

I'm using Nimbus L&F.

Using code from @Romain Hippeau, I had to use UIManager.getLookAndFeelDefaults() instead of UIManager.getDefaults() and use the returned reference to put modified values:

    int szIncr = 5; // Value to increase the size by
    UIDefaults uidef = UIManager.getLookAndFeelDefaults();
    for (Entry<Object,Object> e : uidef.entrySet()) {
        Object val = e.getValue();
        if (val != null && val instanceof FontUIResource) {
            FontUIResource fui = (FontUIResource)val;
            uidef.put(e.getKey(), new FontUIResource(fui.getName(), fui.getStyle(), fui.getSize()+szIncr));
        }
    }

For some reason, it does not seem to work with the default L&F... (based on the limited tests I performed)

Matthieu
  • 2,736
  • 4
  • 57
  • 87
0

To solve this problem, I just implement AWTEventListener and listen for COMPONENT_ADDED of ContainerEvent.

All story description at: http://wiki.idempiere.org/en/Swing_Miss_Support_Some_Language

All code at: https://bitbucket.org/hieplq/unicentapos/src/9b22875ab65e26ff46fd9ae62d556b7f64621afa/src-extend/vn/hsv/uitil/font/FontGlyphsUtil.java?at=tip

  1. Implement AWTEventListener

 

public void eventDispatched(AWTEvent event) {
    if (!isMissSupportGlyph || !(event instanceof ComponentEvent) || !(event instanceof ContainerEvent))
        return;

    if (event instanceof ContainerEvent){
        ContainerEvent containerEvent = (ContainerEvent)event;
        if (containerEvent.getID() == ContainerEvent.COMPONENT_ADDED){
            updateChildControlFont(containerEvent.getChild());
        }
    }
}
  1. Add registry listener (the best place to run this is when starting the program)

 

Toolkit.getDefaultToolkit().addAWTEventListener(this, AWTEvent.COMPONENT_EVENT_MASK | AWTEvent.CONTAINER_EVENT_MASK);
josliber
  • 43,891
  • 12
  • 98
  • 133
Hiep Lq
  • 1
  • 1
0

I used the Synth look and feel XML file and defined the fonts there. Easy, flexible and continent.
You need to create a class with a createFont like:

public class CustomFontResource {
public static FontUIResource createFont(String path, final int size) throws IOException, FontFormatException {
    Font font = Font.createFont(Font.TRUETYPE_FONT, new FileInputStream(path));
    return new FontUIResource(font.deriveFont(Font.PLAIN, size));
}

And in your synth xml define the font like:

    <object id="Basic_Regular" class="<your CustomFontResource class>"
        method="createFont">
    <string>path_to_your_font</string>
    <int>font_size</int>
</object>

then you may use it as a reference wherever you want in the xml.

Hanan N
  • 51
  • 1
  • 4
-1

None of these solutions work fine for me, I built my own (stupid) one but it works:

private void changeFontRecursive(Container root, Font font) {
    for (Component c : root.getComponents()) {
        c.setFont(font);
        if (c instanceof Container) {
            changeFontRecursive((Container) c, font);
        }
    }
}
Adriano
  • 3,788
  • 5
  • 32
  • 53