How to install custom fonts from css in JavaFX?

I added a custom font to my project in the src/main/resources( RobotoMono.ttf) folder . Then I try to load it from my css file as follows:

@font-face {
    font-family: 'RobotoMono';
    src: url('RobotoMono.ttf');
}

but when I try to install this font on something, it does not work:

.column-header-label {
    -fx-label-padding: 0;
    -fx-text-overrun: clip;
    -fx-font-family: 'RobotoMono';
}

If I set the font to something that is present on my system, I see that the font is changing, but the one that is turned on by me does not work.

What am I doing wrong?

+4
source share
3 answers

In your css you must specify a face-font with a url. Then in the css element you should use the font name inside the ttf file, not the name of the font file itself. For example, if you open the ttf file from Windows, you see "Roboto Mono".

@font-face {
    src: url('RobotoMono.ttf');
}

.column-header-label {
    -fx-label-padding: 0;
    -fx-text-overrun: clip;
    -fx-font-family: 'Roboto Mono';
}
+1

, src:

src: url('RobotoMono.ttf');

RobotoMono.ttf , CSS. JavaFX Maven,

SRC///

, , :

Project Folder Structure

+1

just add the font to the java assembly libraries for example, in eclipse you should right click on the project->properties->java build path->add JARs, and then add your ttf font. after you have done this, you just need to add the line below to your css

-fx-font-family: 'name of the font you have added to library without .tff';

make sure your ttf font is inside your project folder

+1
source

Source: https://habr.com/ru/post/1693188/


All Articles