Swing - How to add a custom font to the JTextArea?
My tool basically reads PDF file and prints PDF content to JTextArea
. Everything works fine until my PDF contains PH Mirjan fonts for Arabic. My text area is showing some junk character as shown below.
How can I solve this?
My default font for textarea is Arial Unicode MS . Is there anyway I can customize the font of the text area? Let's say I downloaded PH Mirjan to my local, how do I change the font of the textarea to the loaded one. Any advice or link links are greatly appreciated.
EDIT
try (InputStream is = NewJFrame.class.getResourceAsStream("/GE SS Two Bold.otf"))
{
Font font = Font.createFont(Font.TRUETYPE_FONT, is);
font = font.deriveFont(Font.PLAIN, 24f);
jTextArea1.setFont(font);
jTextArea1.setForeground(Color.BLUE);
and it gives me this exception.
Exception
java.awt.FontFormatException: java.nio.BufferUnderflowException at sun.font.TrueTypeFont.init (TrueTypeFont.java:558) at sun.font.TrueTypeFont. (TrueTypeFont.java:191) at sun.font.CFontManager.createFont2D (CFontManager.java:161) at java.awt.Font. (Font.java:614) to java.awt.Font.createFont0 (Font.java:968) to java.awt.Font.createFont (Font.java:876)
Any idea why I am getting this.
source to share
Why font name .tt
instead of .ttf
? It may not be a file ttf
or is corrupted that an exception occurs
try {
Font NARROW = Font.createFont(Font.TRUETYPE_FONT, this.getClass().getResourceAsStream("/fonts/DSS.ttf"));
NARROW = NARROW.deriveFont(17f);
} catch (FontFormatException | IOException ex) {
System.err.println("Exception loading fonts "+ex);
}
I know this is almost the same code, try it in other files ttf
. Exception
must not be.
source to share