Is it possible to use Font Awesome with swing applications? If possible then how to use its icons with swing components (JButton or JLabel). I've used Font Awesome earlier with my Primefaces application.
Asked
Active
Viewed 1.9k times
22
-
If you can legally distribute the font with the app., Java can most likely load it and register it in the available fonts. Then it is a matter of adjusting the `UIManager` or PLAF to actually *use* that font for buttons, labels or whatever.. – Andrew Thompson Jun 12 '14 at 06:00
-
Possible duplicate of [Setting the Default Font of Swing Program](http://stackoverflow.com/q/7434845/418556) or [How do you import a font?](http://stackoverflow.com/q/8364787/418556) – Andrew Thompson Jun 12 '14 at 06:05
2 Answers
31
I would say "yes"...
- Download the zip package from Font Awesome
- Uncompress it
- Copy the
fontawesome-webfont.ttf
file to your project (in the below example, I used it as an embedded resource) - Using the Cheeatsheet, copy and past the icon you want to use into your code
- Load the font and display...
For example...
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.FontFormatException;
import java.awt.GridBagLayout;
import java.io.IOException;
import java.io.InputStream;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class TestFontAwsome {
public static void main(String[] args) {
new TestFontAwsome();
}
public TestFontAwsome() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
try (InputStream is = TestFontAwsome.class.getResourceAsStream("/fontawesome-webfont.ttf")) {
Font font = Font.createFont(Font.TRUETYPE_FONT, is);
font = font.deriveFont(Font.PLAIN, 24f);
JLabel label = new JLabel("?");
label.setFont(font);
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new GridBagLayout());
frame.add(label);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
} catch (IOException | FontFormatException exp) {
exp.printStackTrace();
}
}
});
}
}
You can also use the unicode directly, for example, the symbol in the above example is listed as 
which could be used as...
JLabel label = new JLabel("\uf0c0");

MadProgrammer
- 343,457
- 22
- 230
- 366
-
1@AndrewThompson Yeah, I was following the instructions on the web page which said to copy the displayed character, added using the unicode directly – MadProgrammer Jun 12 '14 at 06:12
-
Your example worked for me, however I useAction.SMALL_ICON and Action.LARGE_ICON_KEY on my implmentation of Action for toolbar and icons next to action on menu, this expects an Icon not a Font so How do I adapt? – Paul Taylor Mar 22 '23 at 12:00
-
@PaulTaylor The `Action` doesn't support setting of the `Font`. "A" solution would be to generate your own image from the text you want displayed – MadProgrammer Mar 22 '23 at 20:24
-
thx did it https://stackoverflow.com/questions/75812041/how-do-i-use-fontawesome-6-icons-that-are-based-on-a-true-type-font-into-a-icon – Paul Taylor Mar 24 '23 at 11:00
15
Try jIconFont (Swing or JavaFX) at http://jiconfont.github.io/
Example:
Icon icon = IconFontSwing.buildIcon(FontAwesome.SMILE_O, 40, new Color(0, 150, 0));
JLabel label = new JLabel(icon);

Carlos Eduardo
- 740
- 6
- 10