Ankit Ankit - 1 month ago 11
Java Question

Change Font type and size for the whole JFrame?

I am working on Java GUI project. I have several components on the swing-based GUI. I want to change the font settings for all the components, rather than changing font for each component one by one.

Is there a way to change the font type and size of all the components on a JPanel on a JFrame.

Edit:

This is how my code looks:

public class Test extends JFrame{
public Test(){

//all components are initialized here. (some buttons and text fields) and added to this
this.setVisible(true);
}

public static void main(String []args){
new Test();
}

}

Answer
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);
    }
}

// ...

try
{
    setUIFont(new javax.swing.plaf.FontUIResource("Tahoma",Font.PLAIN,12));
}
catch(Exception e){}

PS: I just copied this from old project of mine, and I am not sure from where I got it.