chinna_82
chinna_82

Reputation: 6403

Swing - How to add customized font to JTextArea?

My tool basically read the PDF and print the pdf's content in JTextArea. Everything working fine until my PDF contains PH Mirjan fonts for Arabic. My text area shows some junk character as per below.

enter image description here

How do I solve this?

My default font for the text area is Arial Unicode MS. Is there anyway I can configure the text area's font to take? Let say I downloaded PH Mirjan in my local, how to change the text area font to the downloaded one. Any advice or references links is highly 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 its give 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) at java.awt.Font.createFont0(Font.java:968) at java.awt.Font.createFont(Font.java:876)

Any idea why i'm getting this.?

Upvotes: 3

Views: 982

Answers (1)

Rahul
Rahul

Reputation: 299

Why is the font name is .tt instead of .ttf ? The case might be it is not a ttf file or corrupted that the exception happens

    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 it is pretty much the same code , try this on other ttf files. Exception should not be there.

Upvotes: 1

Related Questions