How to use font awesome in a fxml project (javafx)
I think this is what you need ControlFX that include font awesome support. see the javadoc for more info (But I tested it one day and it works fine)
I achieved using FA Icons by adapting Jens Deters's approach.
His routines target dynamic gui composition opposing fxml's declarative way. Nevertheless, his AwesomeIcon enumeration (which maps FA comprehensible names with unicode characters) suited perfectly to my intents.
It should start by statically load the font in main/app class:
public class App extends Application {
static {
Font.loadFont(App.class.getResource("/font/fontawesome-webfont.ttf").toExternalForm(), 10);
}
@Override
public void start(final Stage primaryStage) throws Exception {
URL resource = getClass().getResource("/fxml/app.fxml");
primaryStage.setScene(new Scene((Parent) FXMLLoader.load(resource), 500, 500));
primaryStage.setTitle("FontAwesomeFX demo");
primaryStage.show();
}
public static void main(String... args){
launch(args);
}
}
One can not use unicode characters in fxml (as needed to specify FA Icons), but can dynamic set attributes with such values. Hence having the above mentioned enumeration (AwesomeIcon), the job was done:
The import:
<?import de.jensd.fx.fontawesome.AwesomeIcon?>
The node:
<Label styleClass="awesome" style="-fx-font-family: FontAwesome; -fx-font-size: 16.0;"> <text><AwesomeIcon fx:constant="FILE"/></text> </Label>
I end up implementing an Icon Widget/Control/Component for resuming the amount of code with two properties:
- value: FA Icon Name;
- size: styleable attribute for style
-fx-font-size
on label.
New code (same effect):
<Icon value="FILE" size="16"/>
The code for that control can be found here. You can also find an working example as it includes the font and testing code.
I ported the Android-Iconics library, developed by Mike Penz, to FX. Updates will follow soon (docs, as well)..
The showcase.jar will give you an overview of the icons.
Usage (Java 1.8 required):
FxIconicsLabel labelTextDefault =
(FxIconicsLabel) new FxIconicsLabel.Builder(FxFontGoogleMaterial.Icons.gmd_folder_special)
.size(24)
.text("Right (default)")
.color(MaterialColor.ORANGE_500)
.build();
(or see DialogPlayGround.java)
FxIconics on GitHub