JavaFX – Register Font
September 7, 2009 6 Comments
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.
Problem:
- 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
Solution:
- Use Java Reflection APIs to invoke registerFont method
- Use JavaFX Asynchronous APIs so as to execute above method in background thread
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
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.
|
Try it out and let me know feedback
var dzone_style = ’2′;







