javafx @font-face css error "loadStyleSheetUnprivileged"
Asked Answered
C

2

1

I'm trying to load a custom font in JavaFx css using this method

@font-face {
    font-family: 'Roboto';
    src: url('fonts/Roboto-Medium.ttf');
}

I've done every thing proper with the correct path but I'm getting this error

Nov 28, 2015 4:49:18 PM com.sun.javafx.css.StyleManager loadStylesheetUnPrivileged
INFO: Could not load @font-face font [file:/C:/Users/RootUser/Desktop/Java8%20projects/RemoteViewer/out/production/JavaFxApplication/application/fonts/Roboto-Medium.ttf]

This is my project structure "screenshot"

enter image description here

Note I'm using intellij Idea as my IDE.Thanks in advance.

Cruelty answered 28/11, 2015 at 16:57 Comment(14)
Where is the CSS file located in your project? And where is the ttf located?Salisbury
@Salisbury It is located in the src package just as the fonts folder, I don't think the problem is with the path coz i have cross checkedCruelty
Try with /fonts/Roboto-Medium.ttf. You should edit your post with the location of each file involved though, it is very important is problem like these.Salisbury
@Salisbury If I do that,the IDE is returning Cannot resolve file Roboto-Medium.ttfCruelty
@ColnsAbt Can you add the project structure in the question?Thilde
@Thilde I have uploaded a screen shot of my project structure, I guess that will add some detail.Cruelty
@ColnsAbt According to the screenshot, the path is correct. Can you check if the css file is also present in the out directory? My wild guess would be that IntelliJ is ignoring resources and not copying them to the output directory.Thilde
@Thilde Yeah,It's present out>production>JavaFxApplication>application>application.css, By the way, this is also the first time I'm encountering this error and it seems unusualCruelty
@ColnsAbt Are the fonts available?Thilde
@Thilde Yeah,every thing is available,and I'm also surprised by the outcome.Cruelty
Which JDK version are you using? Also, try without the font-family.Salisbury
@Salisbury I'm using JDK8, and also the whole application runs perfectly but can't load the custom fonts.Cruelty
I understand you're using Java 8 but JavaFX is tricky so you need to specify the precise version (output of java -version).Salisbury
@Salisbury I only have one JDK on the system, I uninstalled all the rest.Cruelty
L
7

This a bug in JavaFX (8). If there's a space in the font file's pathname, it won't load, and throw an exception, as it happened in with you.

This means that we shouldn't load fonts from a CSS file, unless we're sure the file path won't contain any spaces.

There is a better, safe way to load fonts (~first line inside main()):

Font.loadFont(getClass().getResourceAsStream("/font.ttf"), 16);
Lumen answered 19/1, 2017 at 22:46 Comment(2)
Thanks, also found this out the hard wayCruelty
@CollinsA Oh sorry, this question is a year old. Though it will help for others.Lumen
D
0

For others who have come to this answer when searching:

The same error also occurs if you accidently leave out the quotes for the font name in a style, e.g.:

-fx-font-family: Monserrat;

is wrong, it needs to be

-fx-font-family: 'Montserrat';
Dowson answered 28/5, 2019 at 3:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.