Okay so I have a .ttf folder for a custom font that I wish to load. How do I import it, then use it in a JLabel? I’ve sat here and tried over and over and never got it:/
Here is how I load a .ttf file.
// Font manager should be replaced with your class name.
public static Font createFont(String path, float size) {
try (InputStream is = FontManager.class.getClass().getResourceAsStream(path)) {
Font font = null;
try {
font = Font.createFont(Font.TRUETYPE_FONT, is);
} catch (FontFormatException | IOException e) {
e.printStackTrace();
}
if (font != null) {
font = font.deriveFont(size);
}
return font;
} catch (IOException e1) {
e1.printStackTrace();
}
return null;
}
// Example of me creating it later on.
// /fonts/ is located within my res folder.
Font title_font = FontManager.createFont("/fonts/PHATONE_.TTF", 58); //$NON-NLS-1$
If you are not running Java 1.7 you will need to modify the first line. I am unaware if this is the best design but hopefully it helps; please ask any questions you might have.