Setting the Default Font of Swing Program
Solution 1
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));
Solution 2
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
Solution 3
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.
Solution 4
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
Solution 5
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);
}
}
Connor Neville
I'm an aspiring Java programmer and web/graphic designer in my senior year of high school. That's really it. I'm not particularly significant. Alright then.
Updated on February 12, 2020Comments
-
Connor Neville over 4 years
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 withLookAndFeel
, but I can't find specifically how to do it, and theUIManager
appears pretty complicated. -
Connor Neville almost 13 yearsFor some reason, this is only changing the font on the text of the tabs on my JTabbedPane.
-
gumuruh over 12 yearswaw... 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?
-
Emmanuel Touzery over 11 yearsworks 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)
-
flup almost 11 yearsSource's source: BasicLookAndFeel.java
-
user1568901 over 10 yearsDoesn't work at all for me. Throws unrecognized option errors.
-
Matthieu almost 10 yearsI had it working using
UIManager.getLookAndFeelDefaults()
instead ofUIManager.getDefaults()
and using the returned reference instead ofUIManager.put()
(see my answer below). It works only when overriding the default L&F though (Nimbus in my case)... -
Thufir almost 10 yearsworks fine for a netbeans swing app made with palette, drag and drop.
-
matthiasboesinger over 9 yearsAdditional 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*/);
andUIManager.put("OptionPane.buttonFont", /*font*/);
-
Nicolas GOUDARD about 8 yearsSorry I think this code is better : UIManager.put("Button.font", new FontUIResource("Helvetica", Font.BOLD, 16));
-
Lumii over 7 yearsA more accurate way will be
UIManager.put(key, new javax.swing.plaf.FontUIResource(fr.deriveFont(fr.getSize2D() * 2.0f)));
-
Asce4s over 7 years@gumuruh it works when you refresh the content tree
SwingUtilities.updateComponentTreeUI(this);
-
yolob 21 almost 4 yearsnote calling it BEFORE invoking the ui is necessary else only few places the font effect takes place.