How to use font awesome in a fxml project (javafx)

2020-01-26 10:50发布

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.

6条回答
狗以群分
2楼-- · 2020-01-26 10:55

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)

查看更多
该账号已被封号
3楼-- · 2020-01-26 11:00

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

查看更多
Rolldiameter
4楼-- · 2020-01-26 11:02

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

查看更多
我命由我不由天
5楼-- · 2020-01-26 11:06

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

查看更多
唯我独甜
6楼-- · 2020-01-26 11:10

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!

查看更多
地球回转人心会变
7楼-- · 2020-01-26 11:13

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.

查看更多
登录 后发表回答