Issue
I've custom fonts for my Java SE project so I don't want to install them on client's machine so how can I have fonts and use within project.
Is there a way to accomplish this?
Solution
I have several fonts packaged with my project and I retrieve them as follows:
//Load your font from file
Font myFont = Font.createFont(Font.TRUETYPE_FONT, myClass.class.getClass().getResource("/res/Template/GOTHIC.TTF").openStream());
//set the style and size
myFont = myFont.deriveFont(style, size);
//Now use the font
myTextArea.setFont(myFont);
You do not necessarily have to use getResource()
, you could load it as a File
instead. Don't forget to use the correct font type, not all fonts are Truetype like my example. And obviously you don't want to load the font every time you need it, so just load it once and keep an instance of each font and have a simple method call to your font.
Source: https://docs.oracle.com/javase/8/docs/api/java/awt/Font.html#createFont-int-java.io.InputStream-
Answered By - sorifiend