2

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

Oleg
  • 161
  • 1
  • 14

2 Answers2

2

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.

stryba
  • 1,979
  • 13
  • 19
  • Yes I think your guess is good but is there a way to force the text to be displayed anyway ? – Oleg Oct 07 '14 at 12:07
  • 1
    http://stackoverflow.com/questions/5808195/removing-the-three-dots-from-a-jbutton – stryba Oct 07 '14 at 12:09
0

I found the problem, I removed

save_btn.setPreferredSize( buttonDimension );

And now it works fine !

enter image description here

Oleg
  • 161
  • 1
  • 14
  • You could also set the margin insets to 0. That way the margins to the left and right will come in (if that's what you were attempting to do with the `setPreferredSize`... `button.setMargins(new Insets(0, 0, 0, 0));` – Paul Samsotha Oct 07 '14 at 14:02