Configure Default Font of Swing

I was wondering how to set the default font for my entire Java swing program. From my research, it seems that this can be done using the UIManager , something related to LookAndFeel , but I can not find specifically how to do this, and the UIManager looks rather complicated.

+49
java fonts swing uimanager
Sep 15 '11 at 17:13
source share
11 answers

to 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...

 setUIFont (new javax.swing.plaf.FontUIResource("Serif",Font.ITALIC,12)); 
+41
Sep 15 '11 at 17:21
source share
 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

+32
Sep 15 '11 at 17:18
source share
 java -Dswing.aatext=true -Dswing.plaf.metal.controlFont=Tahoma -Dswing.plaf.metal.userFont=Tahoma … 

This will not only install Tahoma in your full user interface, but also enable anti-aliasing, which will make any font more beautiful right away.

+14
Sep 15 '11 at 18:15
source share

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

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

Somewhere basically before creating a JFrame object. It worked great for me. Remember that this is the default font for components that do not have the specified font.

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

+10
Jul 24 2018-12-12T00:
source share

Keep in mind that the way you set the default font depends on how you look. The solution described by Romain Hippow works great with a lot of LAF, but not with a halo. The one sent by sherif works great with a halo, but not with others (e.g. Metal).

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

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

+4
Jun 22 '14 at 15:48
source share

Inspired by Roman Hippo, use this code if you want to set only 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); } } 
+3
Aug 15 '13 at 18:20
source share

I am using Nimbus L & F.

Using the code from @Romain Hippeau, I had to use UIManager.getLookAndFeelDefaults() instead of UIManager.getDefaults() and use the returned link to put changed 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 doesn't seem to work with standard L & F ... (based on limited tests performed)

0
Aug 21 '14 at 16:28
source share

To solve this problem, I just implement AWTEventListener and listen to COMPONENT_ADDED from ContainerEvent.

All plot description: http://wiki.idempiere.org/en/Swing_Miss_Support_Some_Language

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

  • Implementing 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 a registrar (the best place to start when starting the program)

 Toolkit.getDefaultToolkit().addAWTEventListener(this, AWTEvent.COMPONENT_EVENT_MASK | AWTEvent.CONTAINER_EVENT_MASK); 
0
Jun 13 '15 at 13:32
source share

none of these solutions work for me, I build my own (stupid), 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); } } } 
0
Sep 11 '15 at 9:26
source share

The correct answer is the question of Amir Raminfar, but you must encapsulate the font FontUIResource. For example:

 UIManager.put("Button.font", new FontUIResource(new Font ("Helvetica", Font.BOLD, 16))); 
0
Apr 23 '16 at 11:44
source share

I used the Synth look and feel XML file and defined fonts there. Lightweight, flexible and continental.
You need to create a class with createFont as follows:

 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 as:

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

then you can use it as a link wherever you want in xml.

0
Dec 13 '16 at 9:47
source share



All Articles