Change JOptionPane and JFrame titlebar font

1.3k Views Asked by At

I have to specify the Jframe and JOptionPane elements' title bar to a Cambodian(unicode) text data.
The default font(dialog) doesn't render the text correctly, and I want to override the default font to something else.
Please give a hand, how can I override it?
I tried the followings, none of them worked. (LaF: Substance 7.2.1)

UIManager.put("OptionPane.font", new FontUIResource(Main.get_ui_font()));
UIManager.put("TitledBorder.font", Main.get_ui_font());
UIManager.put("Frame.font", Main.get_ui_font());

I tested above with both Font and FontUIResource, no difference!
The following code throws an internal substance api exception, it's about nullpointerexception, but with full stack, I cannot see any stack trace of my code, so weird, maybe a bug.

SubstanceLookAndFeel.setFontPolicy(new FontPolicy() {public FontSet getFontSet(String arg0, UIDefaults arg1) {
                FontSet fs=new FontSet() {
                    public FontUIResource getWindowTitleFont() {return new FontUIResource(Main.get_ui_font());}
                    public FontUIResource getTitleFont() {return new FontUIResource(Main.get_ui_font());}
                    public FontUIResource getSmallFont() {return new FontUIResource(Main.get_ui_font().deriveFont(10.0F));}
                    public FontUIResource getMessageFont() {return new FontUIResource(Main.get_ui_font());}
                    public FontUIResource getMenuFont() {return new FontUIResource(Main.get_ui_font());}
                    public FontUIResource getControlFont() {return new FontUIResource(Main.get_ui_font());}
                };
                return fs;
            }
        });

Full Stack trace of above code

Exception in thread "main" java.lang.NullPointerException
    at org.pushingpixels.substance.api.SubstanceLookAndFeel.setSkin(SubstanceLookAn
dFeel.java:2150)
    at org.pushingpixels.substance.api.SubstanceLookAndFeel.access$000(SubstanceLoo
kAndFeel.java:92)
    at org.pushingpixels.substance.api.SubstanceLookAndFeel$3.run(SubstanceLookAndF
eel.java:2136)
    at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:301)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:744)
    at java.awt.EventQueue.access$400(EventQueue.java:97)
    at java.awt.EventQueue$3.run(EventQueue.java:697)
    at java.awt.EventQueue$3.run(EventQueue.java:691)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.ja
va:75)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:714)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java
:201)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:11
6)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java
:105)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
1

There are 1 best solutions below

3
On

Could you post the code of the Main.get_ui_font() method?

The task to draw the borders of JFrames and JDialogs windows is delegated to an external windowing system so I'm not so sure you can easly change them.

Furthermore:

  • Frame.font is not a valid key.
  • TitledBorder.font has nothing to do with the titles in JFrames, take a look to the documentation.
  • OptionPane.font changes the font of the message inside the OptionPane

Anyway, for change a font with UIManager.put() you should give as parameters the key and a FontUIResource. So, I'm not sure what your Main.get_ui_font() returns, but considereng the constructors of FontUIResource -- FontUIResource(Font font) and FontUIResource(String name, int style, int size) -- if returns Font than use 1; otherwise, if your method returns FontUIResource, use 2.

  1. UIManager.put("key", new FontUIResource(Main.get_ui_font()));
  2. UIManager.put("key", Main.get_ui_font());

Remember also that if you want to use a "non standard" font you should register it before to use it. For this use GraphicsEnvironment.getLocalGraphicsEnvironment().registerFont(..)