Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Font Awesome with Swing

Tags:

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.

like image 906
Akshay Raut Avatar asked Jun 12 '14 05:06

Akshay Raut


People also ask

Can you animate font awesome icons?

Sure! It doesn't matter if it's a font icon or an SVG icon, you can still animate the element with CSS.

How do I rotate font awesome icons?

Use the fa-spin class to get any icon to rotate, and use fa-pulse to have it rotate with eight steps. This works especially well with fa-spinner & everything in the spinner icons category.


2 Answers

I would say "yes"...

enter image description here

  • 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");
like image 168
MadProgrammer Avatar answered Oct 29 '22 16:10

MadProgrammer


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);
like image 25
Carlos Eduardo Avatar answered Oct 29 '22 18:10

Carlos Eduardo