1

I'm using JavaFX version 8.0.40-b27 and trying to embed a custom/external font via CSS. I've also tried programmatic approaches, all of which have failed. A System.out.print of "font" returns null, which I suspect to be the cause.

Java:

Font font = Font.loadFont( Main.class.getClassLoader().getResourceAsStream( "application/stratum.ttf"), 10);
System.out.println(font); // Prints "null"
nowPlayingTitle.setFont(font);

CSS:

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

.text{
  -fx-font-family: "stratum", "Segoe UI Light";
    -fx-font-weight: 100;
    -fx-text-fill: white;
}

Directory: https://i.stack.imgur.com/c92ii.png

EDIT: System.out.println(font); now prints Font[name=StratumNo1-Thin, family=StratumNo1, style=Thin, size=10.0], so the file is being accessed correctly. However the font is still not being rendered on screen: https://i.stack.imgur.com/bueUk.png

Acme Zx
  • 11
  • 1
  • 3
  • Related question: [How to embed .ttf fonts is JavaFx 2.2?](http://stackoverflow.com/questions/16855677/how-to-embed-ttf-fonts-is-javafx-2-2). As James points out in his answer, your font resourceAsStream parameter is specifying an incorrect location for your project layout. – jewelsea May 14 '15 at 19:52

1 Answers1

2

For the URL in Java code, try either

// relative to classpath, with leading /
Font font = Font.loadFont( Main.class.getClassLoader().getResourceAsStream( "/application/stratum.ttf"), 10);

or

// relative to class:
Font font = Font.loadFont( Main.class.getClassLoader().getResourceAsStream( "stratum.ttf"), 10);

The CSS looks right to me... are you sure your ttf file is being deployed along with the compiled code as css, etc?

James_D
  • 201,275
  • 16
  • 291
  • 322
  • Hi James, thanks for your answer. So I tried the second suggestion, and `System.out.println(font);` now prints out `Font[name=StratumNo1-Thin, family=StratumNo1, style=Thin, size=10.0]`. However, the actual typeface is still not being rendered in the application window (see edit). It seems like the system default font (Lucida Grande) is still used. – Acme Zx May 14 '15 at 21:10
  • I made sure that the css was implemented with the .ttf properly. It looks like the following: `scene.getStylesheets().add( getClass().getResource("application.css").toExternalForm());` – Acme Zx May 14 '15 at 21:11