Specifying External Font in Javafx CSS

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:

  1. It needs to be first in your CSS file.

  2. It cannot use a leading slash in the src URL.

  3. The src URL must locate either

    • a web font via a valid full URL, e.g. http:, https: or file: protocols OR
    • to get it as a resource, it must be a relative path to the CSS file containing the @font-face directive.

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

  1. 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.

  2. 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



Leave a reply



Submit