Oleg
Oleg

Reputation: 179

Font Awsome on JButton

I am trying to display font awesome icons on a JButton. I did the following

Font font = null;
try
{
    font = Font.createFont(Font.TRUETYPE_FONT, new File("../icons/fontawesome-webfont.ttf"));
    font = font.deriveFont(18f);

    GraphicsEnvironment ge = 
        GraphicsEnvironment.getLocalGraphicsEnvironment();
    ge.registerFont(font);
}
catch ( FontFormatException ex ){ex.printStackTrace();}
catch ( IOException ex ){ex.printStackTrace();}

private JButton save_btn = new JButton();
save_btn.setFont(font);
save_btn.setText("\uf0c7");
save_btn.setPreferredSize( buttonDimension );

But everything I get is enter image description here

If I use the font with a JTextArea, it works fine :

JTextArea t = new JTextArea();
t.setRows( 2);
t.setColumns( 12);
t.setFont( font );
t.setText( "\uF0F3 \uF1EC \uf0f3 \uf1ec");
buttonPanel.add(t);

I get enter image description here

Upvotes: 2

Views: 581

Answers (2)

Oleg
Oleg

Reputation: 179

I found the problem, I removed

save_btn.setPreferredSize( buttonDimension );

And now it works fine !

enter image description here

Upvotes: 0

stryba
stryba

Reputation: 2027

My guess is that your button dimensions are too small for displaying the symbol you want to display and the button reverts to showing "..." which don't seem to have good representations in font-awesome.

Upvotes: 2

Related Questions