Setting the Default Font of Swing Program
Asked Answered
F

12

77

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.

Flowage answered 15/9, 2011 at 17:13 Comment(1)
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 issuePitre
C
75

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));
Capillarity answered 15/9, 2011 at 17:21 Comment(2)
For some reason, this is only changing the font on the text of the tabs on my JTabbedPane.Flowage
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)...Gunning
R
46
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

Raconteur answered 15/9, 2011 at 17:18 Comment(4)
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?Disadvantageous
Source's source: BasicLookAndFeel.javaDonetsk
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*/);Engvall
@Disadvantageous it works when you refresh the content tree SwingUtilities.updateComponentTreeUI(this);Wrongdoer
B
24
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.

Blenny answered 15/9, 2011 at 18:15 Comment(2)
works great. And to set a font which has spaces in its name you can quote. Also specify the font size by adding a "-": -Dswing.plaf.metal.controlFont="Droid Sans-15" (size 15)Slipper
Doesn't work at all for me. Throws unrecognized option errors.Acriflavine
C
15

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

Conciliatory answered 24/7, 2012 at 12:51 Comment(1)
works fine for a netbeans swing app made with palette, drag and drop.Boredom
U
7

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);
    }
}
Underplay answered 15/8, 2013 at 18:20 Comment(1)
A more accurate way will be UIManager.put(key, new javax.swing.plaf.FontUIResource(fr.deriveFont(fr.getSize2D() * 2.0f)));Radiothermy
M
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.

Megillah answered 22/6, 2014 at 15:48 Comment(0)
H
4

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

Haggar answered 2/5, 2018 at 2:20 Comment(1)
note calling it BEFORE invoking the ui is necessary else only few places the font effect takes place.Cerallua
C
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)));
Costumier answered 23/4, 2016 at 11:44 Comment(1)
Sorry I think this code is better : UIManager.put("Button.font", new FontUIResource("Helvetica", Font.BOLD, 16));Costumier
G
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)

Gunning answered 21/8, 2014 at 16:28 Comment(0)
M
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);
Mons answered 13/6, 2015 at 13:32 Comment(0)
C
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.

Cordiecordier answered 13/12, 2016 at 9:47 Comment(0)
C
-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);
        }
    }
}
Conjunct answered 11/9, 2015 at 9:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.