Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Why does Font.getFont return null when looking for system font?

843806May 5 2009 — edited May 5 2009
I have the following code that looks for a font that I know is installed (see below). What am i doing wrong?
//Calling this, returns 9among other fonts): "java.awt.Font[family=SansSerif,name=SansSerif.bold,style=plain,size=1]"
Font[] fonts = GraphicsEnvironment.getLocalGraphicsEnvironment().getAllFonts();
for ( int i = 0; i < fonts.length; i++ ) System.out.println( fonts[ i ].getFontName() );

//Using the above results, I tried ALL of the following but they're ALL null!
Font.getFont( "java.awt.Font[family=SansSerif,name=SansSerif.bold,style=plain,size=1]" );
Font.getFont( "SansSerif" );
Font.getFont( "SansSerif.bold" );
Font.getFont( "family=SansSerif,name=SansSerif.bold,style=plain,size=1" );
Font.getFont( "SansSerif.bold-plain-1]" );
Font.getFont( "SansSerif-plain-1" );

/* Most of the above were chosen by the javadoc info:

To ensure that this method returns the desired Font, format the str parameter in one of these ways 

fontname-style-pointsize 
fontname-pointsize 
fontname-style 
fontname 
fontname style pointsize 
fontname pointsize 
fontname style 
fontname
*/
What am I doing wrong?

UPDATE: looking further into the JRE's source code, I see the getFont method is this:
public static Font getFont(String nm, Font font) {
        String str = null;
        try {
            str =System.getProperty(nm);
        } catch(SecurityException e) {
        }
        if (str == null) {
            return font;
        }
        return decode ( str );
    }
I don't get it, how does that ever work? I've never seen the fonts in the system properties on any computer in any version of java. Testing on these setups, they all return null and none of them have any fonts in the system properties:

* OpenSuse - Java 6
* OpenSuse - Java 5
* Windows XP - Java 6
* Windows XP - Java 5

Does anyone on here have this method work?
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jun 2 2009
Added on May 5 2009
1 comment
860 views