14

I want to use font font awesome in my project but I have no idea how to use font awesome in my project.

I had found some example but they can't be use in fxml.

font awesome javafx

I need help how to use it in my project using fxml

Thank you.

Amir
  • 8,821
  • 7
  • 44
  • 48
user3749316
  • 182
  • 1
  • 1
  • 14

6 Answers6

15

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:

  1. The import:

    <?import de.jensd.fx.fontawesome.AwesomeIcon?>
    
  2. 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:

  1. value: FA Icon Name;
  2. 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.

  • 1
    I tried this, but getting weired symbols instead of actual icons. Sorry i am new to Java and JavaFx. Also i am using exactly the same version of FontAwesom specified in your code "Icon Enum based on Font-Awesome v4.1.0" – ATHER Sep 16 '14 at 17:25
  • Perhaps you missed some step. I just updated code to present an working example. – Nuno Rafael Figueiredo Sep 18 '14 at 18:20
  • Thanks i will check that out. Also when i used '' SceneBuilder no longer opens my fxml and throws error. Is this normal ? – ATHER Sep 18 '14 at 18:53
  • This has stopped working with FontAwesome version 5. Would appreciate some inputs on my question about that: https://stackoverflow.com/questions/59974248/how-to-upgrade-fontawesome-to-version-5-in-javafx – Renato Jan 29 '20 at 19:40
9

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)

Glory
  • 1,007
  • 9
  • 14
agonist_
  • 4,890
  • 6
  • 32
  • 55
6

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

enter image description here enter image description here

Martin Pfeffer
  • 12,471
  • 9
  • 59
  • 68
  • Creating an application in JavaFX for student project, I got your jar installed however my program crashes when running the line with FxIconicsLabel. Did I need to port something outside of your github page and how if so? Very new to JavaFX. – arinh Oct 05 '15 at 07:13
  • 1
    Turns out I had JDK 1.7 so I updated to 1.8 and problem fixed. Thank you for sharing this! – arinh Oct 05 '15 at 07:24
4

If you use SceneBuilder try this.

  • First, download 'fontawesomefx'.
  • Second, import jar to SceneBuilder using SceneBuilder's Jar/FXML Manager.
  • Third, Library search for FontAwesomeIconView, GlyphCheckBox, MaterialDesignIconView, MaterialIconView, or WeatherIconView

Sample FXML:

<?xml version="1.0" encoding="UTF-8"?>

<?import de.jensd.fx.glyphs.control.GlyphCheckBox?>
<?import de.jensd.fx.glyphs.fontawesome.FontAwesomeIconView?>
<?import de.jensd.fx.glyphs.materialdesignicons.MaterialDesignIconView?>
<?import de.jensd.fx.glyphs.materialicons.MaterialIconView?>
<?import de.jensd.fx.glyphs.weathericons.WeatherIconView?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.StackPane?>
<?import javafx.scene.layout.VBox?>


<StackPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.141" xmlns:fx="http://javafx.com/fxml/1">
   <children>
      <VBox maxHeight="-Infinity" maxWidth="-Infinity">
         <children>
            <Label text="FontAwesomeIconView">
               <graphic>
                  <FontAwesomeIconView />
               </graphic>
            </Label>
            <Label text="GlyphCheckBox">
               <graphic>
                  <GlyphCheckBox />
               </graphic>
            </Label>
            <Label text="MaterialDesignIconView">
               <graphic>
                  <MaterialDesignIconView />
               </graphic>
            </Label>
            <Label text="MaterialIconView">
               <graphic>
                  <MaterialIconView />
               </graphic>
            </Label>
            <Label text="WeatherIconView">
               <graphic>
                  <WeatherIconView />
               </graphic>
            </Label>
         </children>
      </VBox>
   </children>
</StackPane>

enter image description here

Don't forget to add these jars to your project's classpath!

SedJ601
  • 12,173
  • 3
  • 41
  • 59
4

As said by @Sedrick you can use fontawesomefx library and use it in FXML as follows:

Note: JavaFX 8 and FontAwesomeFx v8.9

dashboard.fxml

<?xml version="1.0" encoding="UTF-8"?>

<?import de.jensd.fx.glyphs.fontawesome.FontAwesomeIconView?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.layout.AnchorPane?>

<AnchorPane xmlns="http://javafx.com/javafx/8.0.121" 
    xmlns:fx="http://javafx.com/fxml/1"
    fx:controller="com.example.DashboardController" 
    prefHeight="760" prefWidth="1080">

    <Button text="Close" AnchorPane.topAnchor="0" AnchorPane.leftAnchor="0">
        <graphic>
            <FontAwesomeIconView glyphName="CLOSE" glyphSize="24"/>
        </graphic>
    </Button>

</AnchorPane>

In scene builder looks like below: screenshot of iconised button

Omkar
  • 1,493
  • 3
  • 17
  • 36
1

You can use fontawesomefx library by using .jar file in Scene Builder and you can browse all available icons with Fontawesome-fx Glyph browser

Amine ABBAOUI
  • 175
  • 1
  • 12