Adding fonts to Swing application and include in package

DanM picture DanM · Oct 21, 2012 · Viewed 18.1k times · Source

I need to use custom fonts (ttf) in my Java Swing application. How do I add them to my package and use them?

Mean while, I just install them in windows and then I use them, but I don't wish that the usage of the application will be so complicated, it`s not very convenient to tell the user to install fonts before using my application.

Answer

Reimeus picture Reimeus · Oct 21, 2012

You could load them via an InputStream:

InputStream is = MyClass.class.getResourceAsStream("TestFont.ttf");
Font font = Font.createFont(Font.TRUETYPE_FONT, is);

This loaded font has no predefined font settings so to use, you would have to do:

Font sizedFont = font.deriveFont(12f);
myLabel.setFont(sizedFont);

See:

Physical and Logical Fonts