2

Some coworkers and I are writing help documentation in HTML to be used with a JavaFX WebView panel. We want it to look consistent with the rest of the UI. Normally I would specify the default font in a CSS file, but the rest of the JavaFX application appears to use the default system UI font.

Is there any way to set this programmatically? I can pick Segoe UI by name in .css, but we want it to look correct regardless of platform.

Jason S
  • 184,598
  • 164
  • 608
  • 970

2 Answers2

1

Use

Font.getDefault().getName()   

to get the defaults font name and just add it to the HTML file / String which is about to be added into the WebView.

example:

cssString = cssString + "html { font-family: '" + Font.getDefault().getName() + "';}";

Ofcause you should do it cleaner than this.

Marcel
  • 1,509
  • 1
  • 17
  • 39
0

If I understood you completely you want a default font for the whole javafx part of your application. I suggest you do this:

  1. Download a font of your own as .ttf and add it to the resources in your application.
  2. Load the font with the static function Font.loadFont: Font.loadFont(this.class.getClassLoader().getResource("fonts/YOURFONT.ttf").toExternalForm(), 10);
  3. Now set the font to the root of the application. I suggest doing that with css as it is suggested here -> another stackoverflow answer: In your application.css add: .root{ -fx-font-family: "YOURFONT"; } After that you need to load the application.css file and set it on your scene: scene.getStylesheets().add(this.class.getClassLoader().getResource("css/application.css").toExternalForm());

I believe that it is better if you are not dependent of the system on which your app is running, so pack the font in your application.

For the html you can do completely the same thing with normal css by providing a @font-face.

Community
  • 1
  • 1
ALabrosk
  • 300
  • 1
  • 3
  • 12
  • hmm... this seems like unnecessary work; we only want to use system fonts, not provide any .ttf files. – Jason S Dec 10 '15 at 17:50