Set Font globally in JavaFX


Question

How can I set the Font type globally in a JavaFX application?

Is there any solution that I can use? In JavaFX 8 the default Font has changed, and I would like to use the same Font used in JavaFX 2.2.

1
12
9/11/2016 5:59:27 PM

Accepted Answer

You can skin your application with CSS as described on the Oracle Website. Using following syntax you may set the general theme for your application:

.root{
    -fx-font-size: 16pt;
    -fx-font-family: "Courier New";
    -fx-base: rgb(132, 145, 47);
    -fx-background: rgb(225, 228, 203);
}

You include the css as followed:

scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
24
2/3/2014 6:25:27 PM

Changing the Default Font for a Scene

This is the solution outlinked in nyyrikki's answer.

You can change the default font used for most things in any given scene by applying the following CSS stylesheet to the scene:

.root {
  -fx-font: 28px Vivaldi;
}

Substitute whatever settings you require for the -fx-font value according the font definition in the JavaFX CSS reference guide.

Changing the Default Font for an Application

If you want to change the default font used for most things in a JavaFX application, you can override the default style sheet using Application.setUserAgentStylesheet. Using this method you can set the default style for a JavaFX 8 application to the caspian stylesheet which was default for JavaFX 2.2 rather than the modena stylesheet which is default for JavaFX 8. If you want some hybrid of the two default styles or some custom default stylesheet such as AquaFX, then you will need to perform the customization yourself.

Switching Font Rendering Technology

Additionally, on some platforms, JavaFX 2.2 uses a different font rendering mechanism than JavaFX 8, which can account for subtle differences in font rendering between the two. There is an undocumented and unsupported command line switch which can be used to switch between font rendering mechanisms in JavaFX 8, but I don't know what the switch is off-hand and, even if I did, I wouldn't recommend deploying an application using the switch as it is unsupported.


Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon