How to change the default font of Swing components?

2

I would like to know if I have to change the default of the labels fonts and Swing buttons, if possible, of the general design.

All the labels that I create I have to keep changing the font to one that is bigger a bit and another style, this is tiring and I waste a lot of time. I looked hard at how to change and found nothing.

Can anyone help me?

    
asked by anonymous 18.10.2015 / 04:03

1 answer

2

According to this answer in jGuru you can define the source of each resource, only of the components you are interested in - in this case the label and the button .

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 */);

Or you can change the value of the objects in UIManager.getDefault().keys() instances that are FontUIResource .

  

Important: This solution will depend on the Look and feel of your application. Home   If you are using Nimbus , for example, the changes did not take effect.

public void setDefaultFont(Font defaultFont){

   FontUIResource font = new FontUIResource(defaultFont);

   Enumeration uiManagerKeys = UIManager.getDefaults().keys();
   while(uiManagerKeys.hasMoreElements()){
        Object key   = uiManagerKeys.nextElement(),
               value = UIManager.get(key);

        if(null != value && value instanceof FontUIResource)
            UIManager.put(key, font);
   }
}

And make use of it like this:

Font font = new Font("Arial", Font.PLAIN, 25);
setDefaultFont(font);

Result in Windows 10:

    
21.10.2015 / 03:30