Home > javafx > JavaFX – Register Font

JavaFX – Register Font

In one of my previous post I discussed about how to bundle custom font along with application. Recently I came across another query related to registering a custom font. Java API – Font.createFont(int fontFormat, InputStream fontStream) provides a way to create an instance of AWT font. Once created the font needs to be registered so that it can be loaded using the font name or family name. JavaSE 6.0 and above has API – GraphicsEnvironment.registerFont(Font font) for this purpose.


  • API is available only since JavaSE 6.0, it may not be possible to use these APIs directly from JavaFX (which enforce JavaSE 5.0 compatibility)
  • API involves loading of font over network, so we need to perform this operation in background thread so as to avoid freezing the user interface


In below example, font – RRRaghuMalayalam.ttf is downloaded from specified URL and registered. Launch application and click on “Register Font” button. The application is signed since it access resource from another domain.

For Applet mode, click on above image

For standalone mode

Note: You may have to wait for a while until the font is downloaded from the URL and registered. Sample requires JavaSE 6.0 or above.

// Register Font
var registerFontTask = RegisterFontTask {
    url: font-url
    onDone: function() {
        text.font = Font 
            name: "<font-name>" 
            size: 40 

Try it out and let me know feedback

var dzone_style = ‘2’;

Categories: javafx Tags: , ,
  1. September 7, 2009 at 9:47 AM

    Nice trick!

  2. September 7, 2009 at 1:48 PM

    Very clever – the touch of the master πŸ™‚

  3. September 7, 2009 at 8:27 PM

    @Philippe Lhoste Thanks!
    @Ernie Kent Thanks! I’m more of a messenger of masters.. πŸ™‚

  4. September 10, 2009 at 11:35 AM

    Hi, Rakesh. If you can bear with me a bit further I have a question concerning this method. When I first tried it I thought the signing was required because the font was obtained from a different domain, which would not be a problem for us. However I now see that accessing the class in the local JRE also raises a security exception. Do you think it is possible to do something like this but package the method in a jar downloaded with the applet? Thanks!

  5. September 13, 2009 at 8:55 PM

    @Ernie I will look into this. I have sent you an email, please reply with more info.

  6. September 18, 2009 at 1:05 PM

    Thanks, Rakesh
    I did reply to your email last week. Basically the issue is that when using your method a signed applet seems to be required even when the font is loaded from the same place as the applet, The reason appears to be that accessing the JRE6 methods as done in your solution requires security privileges. I have found a different workaround by replacing the JRE5 jar in the JavaFX SDK and the Netbeans JAvaFX extension with a JRE6 jar. Netbeans then still flags an error on screen when RegisterFont is used, but the applet compiles and runs correctly.

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: