Omega
Omega

Reputation: 21

Display FontAwesome icons with JavaFX

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!

Upvotes: 2

Views: 3679

Answers (1)

Yep
Yep

Reputation: 141

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.

Upvotes: 1

Related Questions