4

I have made a swing application. Now I want to change the Font of entire Swing Application , so that any component being added should follow that Font only. Is there any way to achieve the same ?

Abhishek Choudhary
  • 8,255
  • 19
  • 69
  • 128

1 Answers1

10
public static void setGlobalFont( Font font ) {  
        Enumeration keys = UIManager.getDefaults().keys();  
        while (keys.hasMoreElements() ) {  
            Object key = keys.nextElement();  
            Object value = UIManager.get( key );  
            if ( value instanceof Font ) {  
                UIManager.put( key, font );  
            }  
        }  
    }  
Cheok Yan Cheng
  • 47,586
  • 132
  • 466
  • 875
Saurabh Gokhale
  • 53,625
  • 36
  • 139
  • 164
  • 2
    Isn't it a little ... brute force ? – Riduidel Jun 08 '11 at 09:41
  • +1 well, yes, that's correct and in most cases works, but for some JComponents are needed force that with SwingUtilities.updateComponentTreeUI(myFrame/myDialog); – mKorbel Jun 08 '11 at 09:43
  • can't the above piece can't be changed so that [articular components can be provided with some font size differences, like I want JComboBox item having same UI default font size but JTable content with different fontSize – Abhishek Choudhary Jun 08 '11 at 09:53
  • 2
    @Abhishek: Now you are discovering the pitfalls of presuming that 'one `Font` fits all'. ;) To look at those `UIManager` values, and make better choices according to the use, see the [UIManager Defaults](http://tips4java.wordpress.com/2008/10/09/uimanager-defaults/) of the current PLAF. – Andrew Thompson Jun 08 '11 at 10:16