Specifying external font in JavaFX CSS
You were close to the solution:
-fx-font-family: 'Helvetica', Arial, sans-serif;
Try this, it should work fine.
Maybe this could help? (I'm not an expert in JavaFX)
https://forums.oracle.com/forums/thread.jspa?messageID=10107625
EDIT:
Load your font:
@font-face {
font-family: Delicious;
src: url('Delicious-Roman.otf');
}
Use your font with -fx-
:
-fx-font-family: 'Delicious';
How do I set custom fonts from css in JavaFX?
In your css, you have to specify a font-face with a url. Then, in your css element you have to use the name of the font from inside the ttf file, not the name of the font file itself. For instance, 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';
}
Cannot use custom font in javaFX using CSS Styling
@font-face
can work BUT:
It needs to be first in your CSS file.
It cannot use a leading slash in the src URL.
The src URL must locate either
- a web font via a valid full URL, e.g.
http:
,https:
orfile:
protocols OR - to get it as a resource, it must be a relative path to the CSS file containing the @font-face directive.
- a web font via a valid full URL, e.g.
If that is all set up correctly, then the instructions in the following answer work fine (verified with JavaFX 17):
- JavaFX custom Fonts
An example URL that would work for the resources defined in the question is a src URL relative to the CSS file:
src: url("fonts/Pixeboy.ttf");
assuming that the font is actually located at that relative position to the CSS file referencing the @font-face
.
When I tested, I used the CSS and fonts linked from the answer above.
Troubleshooting
If you use a leading slash in the src URL for @font-face (for example
src: url("/fonts/Pixeboy.ttf");
), you will get the following message and the font will not be loaded:Jan 12, 2022 2:31:09 PM javafx.css.CssParser reportException
WARNING: Please report java.lang.NullPointerException at:The error message will be generated and the font will not load even if the path is correct to locate the font file from the root of the class/module path.
If you specify a location for the font which does not exist (for example
src: url("wrongfoldername/Pixeboy.ttf");
, you will get the following error message and the font will not load:Jan 12, 2022 2:43:18 PM com.sun.javafx.css.StyleManager loadStylesheetUnPrivileged
INFO: Could not load @font-face font [file:/<build-output-dir>/wrongfoldername/Pixeboy.ttf]
How to set custom fonts in JavaFX Scene Builder using CSS
Make sure to use Font.loadFont
to actually load the font on application startup. Then you should be able to use the font from CSS. Be careful to use the font's name, not the font file's name. That's a common mistake.
I have used the following before to load and use a custom font:
Font.loadFont(getClass().getResourceAsStream("/resources/fonts/marck.ttf"), 14);
and:
-fx-font-family: "Marck Script";
FYI: the quotes are only needed if the name contains a space.
Can't set custom font
As from JavaFX version 1.8u60
you can use plain css to load the font but with carefull that you need to use the exactly original name of the Font File
,for example:
@font-face{
src: url("../fonts/Younger than me Bold.ttf");
}
.button{
-fx-background-color:white;
-fx-background-radius:15.0;
-fx-font-size:18.0;
-fx-font-family:"Younger than me";
-fx-text-fill:black;
-fx-font-weight:bold;
}
Mention that the original name can be found opening the .ttf
with a default editor and see it's name in case if you don't know it.
Complete tutorial:http://www.guigarage.com/2014/10/integrate-custom-fonts-javafx-application-using-css/
Have a look also here for your situation:Specifying external font in JavaFX CSS
Finally:
The problem in your code is that in the css you are not adding double quotes "
or single quotes '
around the font - family
Related Topics
Override Rmarkdown Theme in Order to Change HTML Page Width
List of CSS Features Not Supported by IE6
CSS Drop Shadow for CSS Drawn Arrow
Bootstrap Carousel as Website Background
Difference Between Width and Device-Width in CSS Media Queries
How to Include Common CSS Class in Another CSS Class
Css3 Animate: How to Have the Object Not Revert to Its Initial Position After Animation Has Run
Parent Height Doesn't Follow Their Float Children
What Are Differences Between Rgb VS Rgba Other Than 'Opacity'
How to Hide Element Label by Element Id in CSS
The "Before" Pseudo Element Not Working in Font Awesome V.5
Bootstrap Modal Window Inside Another Div
Customise Tumblr's *New* Like Button Iframe {Likebutton}
Css3 Gradient Rendering Issues from Transparent to White
Redundant CSS Rules Ie Float & Display:Block
How to Make a Text Stroke with Transparent Text
CSS Change Custom Cursor Image Origin (Hotspot) to Center
How to Standardize the Height of a Select Box Between Chrome and Firefox