2

I'm trying to create a class that could load FontAwesome icons but it doesn't achieve to read the fontawesome-webfont.ttf file.

Class to create an icon:

import javafx.scene.control.Label;
import javafx.scene.text.Font;

public class AppFont {
    static {
        Font.loadFont(AppFont.class.getResource("/res/fontawesome-webfont.ttf").toExternalForm(), 10);
    }

    public static Label createIcon(Icon icon, int iconSize) {
        Label label = new Label(icon.toString());
        label.setStyle("-fx-font-family: FontAwesome; -fx-font-size: " + iconSize + ";");
        return label;
    }
}

Icon enum:

public enum Icon {

    TEST('\uf099');

    private Character c;

    private Icon(Character c) {
        this.c = c;
    }

    public Character getCharacter() {
        return c;
    }

    @Override
    public String toString() {
        return c.toString();
    }
}

Usage:

Label l = AppFont.createIcon(Icon.TEST, 40);
this.getChildren().add(l);

What I get

Only the black rectangle

and of course .ttf file is in the right location. It doesn't throw any error.

Thanks for your help!

Azeem
  • 11,148
  • 4
  • 27
  • 40
Omega
  • 21
  • 1
  • 3
  • You can see: https://stackoverflow.com/questions/24430121/how-to-use-font-awesome-in-a-fxml-project-javafx and http://fxexperience.com/controlsfx/features/ – Vasyl Lyashkevych Jul 08 '17 at 01:06
  • Possible duplicate of [how to use font awesome in a fxml project (javafx)](https://stackoverflow.com/questions/24430121/how-to-use-font-awesome-in-a-fxml-project-javafx) – Jonny Henly Jul 08 '17 at 01:17
  • And I would like to do it without any external jars and fxml – Omega Jul 08 '17 at 10:47

1 Answers1

1

Make sure the -fx-font-family: is set to the correct name for the font you are loading. I am using Font Awesome 5 Pro Regular' or Font Awesome 5 Freeonce you get that name right your code should work.

Yep
  • 141
  • 1
  • 13