I'm working on a JavaFX project. I'd like to use custom font on a Label
from a CSS file. I've read and followd eg. this post.
public class Main extends Application {
@Override
public void init() {
Font font = Font.loadFont(getClass().getResourceAsStream("Dimitri_Swank.ttf"), 32);
System.out.println(font);
}
@Override
public void start(Stage primaryStage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("Window.fxml"));
// ...
}
}
It prints Font[name=Dimitri Swank, family=Dimitri Swank, style=Regular, size=32.0]
, so that the font is loaded successfully. The root container in the FXML file has a stylesheets="@Window.css"
attribute, and in this CSS file, there is a rule:
#gameName {
-fx-font-family: Dimitri Swank;
-fx-font-size: 32px;
}
The selector works well (it refers to a Label
), I can see the difference if I change the -fx-font-size
. But the font family remains the default system font. What am I doing wrong?