1

How do I change an apply a different Font into multiple Components or the entire JFrame or the entire Swing Application, which some Components are already assigned with a Font, after the UI is generated?

The task I'm trying to do is to change and apply a different or custom Font into JFrame upon loading which is in a NetBeans Project therefore some of the Components are already assigned with a Font by its auto generated methods.

So far I have tried setting the Font using the UIManager into its default keys, and then calling SwingUtilities.updateComponentTreeUI(jframe); but it only applied the Font to the Components those have not been assigned with a Font already. Here is how I made the attempt (with the example of this answer.).

        Enumeration enc = UIManager.getDefaults().keys();

        while (en.hasMoreElements() ) {
            Object key = en.nextElement();
            Object value = UIManager.get(key);
            if (value instanceof Font) {
                UIManager.put(key, font);
            }
        }

And then calling,

SwingUtilities.updateComponentTreeUI(jframe);

As it was not entirely successful, then I tried to set the Font in all the Components in the JFrame and then calling validate(); followed by repaint();, but it didn't make any effect whatsoever. This is how I tried to do it.

        Component[] comps = this.getComponents();
        for (Component comp : comps){
            comp.setFont(font);
        }

And then calling,

this.validate();
this.repaint();

Have I done those previous steps correctly or is there any error I have made? If not, what is the correct way to accomplish my task? And also, even though I need to change the Font in all the Components, their existing Font sizes and Font styles are needed to be preserved as well.

Thank you!

Community
  • 1
  • 1
RocketRuwan
  • 223
  • 1
  • 3
  • 14
  • 1
    Could you show the code of "As it was not entirely successful, then I tried to set the Font in all the Components in the JFrame"? For me it should work – StanislavL Jan 17 '14 at 09:45
  • 1
    no issue, for better help sooner post an [SSCCE](http://sscce.org/) / [MCVE](http://stackoverflow.com/help/mcve), short runnable, compilable – mKorbel Jan 17 '14 at 09:55
  • Thanks to both of you for the suggestion and guidance. I have updated the question with the codes I used in my attempts. I hope I did it right. – RocketRuwan Jan 17 '14 at 10:59
  • 1
    updateComponentTree should work - if it doesn't there's something wrong with the code you are not showing (please read the articles @mKorbel reference, your snippets are _not_ what's needed to help you) – kleopatra Jan 17 '14 at 12:26

1 Answers1

4

Change the Pluggable Look and Feel to a (possibly custom) PLAF is the best, most robust way to do it. See this answer for tips on how to change the PLAF after the GUI is visible.

See the Modifying the Look and Feel lesson of the Java Tutorial for further details. See below for examples of one user interface using two different PLAFs.

Community
  • 1
  • 1
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • Thank you for your guidance. So far it looks like the only way I have to do this is by using the `SynthLookAndFeel` and defining the Font, since what I need is to change the entire application's default font into a custom font of my preference, or else I still have to learn more about this. I will update my progress as it happens. Thanks again. I would definitely +1 this but I lack the 15 reputation to do so. – RocketRuwan Jan 17 '14 at 11:20
  • 1
    Haha. Indeed. With your help I no longer lack it. Thank you! :) – RocketRuwan Jan 17 '14 at 11:51
  • Sorry for taking a long time to accept it. It was so hard for me to try out on these with the limited time I've got. However. I must say the path you showed me was absolutely right and I accomplished the task by modifying the LAF, yet I still have a lot to learn and the guidance you gave me will lead me through it. Thanks again! :) – RocketRuwan Mar 06 '14 at 21:34