1

How can I embed a custom font in a JavaFX Application? I've tried making a fonts.mf file, and following the instructions to a similar question linked below. I don't want to use CSS if I don't have to. I want to focus on learning core JavaFX stuff.

Here is what I've been messing around with:

private static Label makeTitle() {
   Label title = new Label("Bandit King");
   Font font = new Font("OldStyle", 40);
   title.setFont(font);

   return title;
}

my fonts.mf file contains just this line:

OldStyle = /home/myName/Desktop/My_Java_Projects/Bandit_King/banditKing/OLDSH.TTF

This is not a duplicate of this question. Eclipse says, "CustomFontApp, cannot be resolved to a type".

LuminousNutria
  • 1,883
  • 2
  • 18
  • 44
  • You don't show your attempt to load the font (i.e. `Font.loadFont`). And what is `CustomFontApp`? – Slaw Oct 11 '18 at 01:31
  • I have no idea what CustomFontApp is, it is just written in the question I linked. I thought `Font font = new Font("OldStyle", 40);` did load the font. – LuminousNutria Oct 11 '18 at 01:32
  • As far as I know, the constructors and factory methods simply search the existing fonts for a best match. You have to actually load any custom fonts with `Font.loadFont` or `@font-face` before use. Also, it looks like `CustomFontApp` is the name of the example `Application` class in the linked non-duplicate; you can't use that in your own code unless you also create that class. – Slaw Oct 11 '18 at 01:55
  • :/ sorry, I should've noticed that. I have another problem. I can't seem to use `toExternalForm()` in the same way. Eclipse says, "The method `toExternalForm()` is undefined for the type InputStream" – LuminousNutria Oct 11 '18 at 02:08
  • Post the code that is causing the errors. – Slaw Oct 11 '18 at 02:14

1 Answers1

1

I've tried making a fonts.mf file

You don't need it.

This is not a duplicate of this question. Eclipse says, "CustomFontApp, cannot be resolved to a type".

CustomFontApp is just the name of a class used in the answer you linked - your class name is obviously different, and you should've changed it.

This line:

Font font = new Font("OldStyle", 40);

will load OldStyle font only if it's installed on your system. You aren't using an installed font, but the embedded one, so that won't work.

You need to use Font.loadFont(InputStream, double) or Font.loadFont(String, double) to load your custom font from disk:

// use this to load font from your application's resource folder (`res/fonts/OLDSIH.TTF`)
Font font = Font.loadFont(getClass().getResourceAsStream("/fonts/OLDSIH.TTF"), 40);

// or this one to load font from the specified (absolute) path
// (not recommended, use the method above or, at least, change this into relative path):
Font font = Font.loadFont("file:///home/myName/Desktop/My_Java_Projects/Bandit_King/banditKing/OLDSH.TTF", 40);
Guest 21
  • 672
  • 1
  • 6
  • 10
  • I did what you said, but Eclipse is throwing an error with `getClass()` It says, "Cannot make a static reference to the non-static method `getClass()` from the type Object". My code is `Font font = Font.loadFont(getClass().getResourceAsStream("/fonts/OLDSH.TTF"), 40);` – LuminousNutria Oct 11 '18 at 20:57
  • That's because `makeTitle` method is `static`, while `getClass()` is an instance method, so it can't be called without an object. You really should move your code from `main` to `start` method, but if you just want a quickfix for a problem above - change `getClass()` into `Main.class`, where `Main` is the name of your main class. – Guest 21 Oct 11 '18 at 21:09