Ankit Ankit - 8 months ago 61
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.


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

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


public static void setUIFont(javax.swing.plaf.FontUIResource f)
    java.util.Enumeration keys = UIManager.getDefaults().keys();
        Object key = keys.nextElement();
        Object value = UIManager.get(key);
        if(value instanceof javax.swing.plaf.FontUIResource) UIManager.put(key, f);

// ...

    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.