My approach involves a mix of application code and CSS to apply styling using an external font.
In order to ensure the loadFont
function is called early on in the application process, I place it within a customized Application init method.
Font.loadFont(CustomFontTest.class.getResource("TRON.TTF").toExternalForm(), 10);
When utilizing the font, I specify the font family in CSS as follows:
.menu-bar {
-fx-background-color: transparent;
-fx-font-family: TRON;
-fx-font-size: 40px;
}
.context-menu {
-fx-font-family: TRON;
-fx-background-color: transparent;
-fx-font-size: 12px;
}
It's noteworthy that the CSS effectively adjusts the font sizes. Even though the font is initially loaded at size 10, it correctly conforms to the specified sizes in the CSS -fx-font-size
declarations.
Applying inline styling to a label via CSS with a loaded Font
during application initialization also functions seamlessly:
Label testControl = new Label("TRON");
testControl.setStyle("-fx-font-family: TRON; -fx-font-size: 120;");
The TRON font was acquired from dafont, stored in the same directory as the CustomFontTest class, and copied to the build output directory by the build system.
This response was retrieved from my contribution on a forum post regarding "Using Custom Fonts".