Set Font globally in JavaFX
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.
FWIW, if you want to make the font change programmatically (no external css files involved), you can make a "global" change by using the .setStyle()
method on the top parent node.
Example, I wrote a quick GUI (a bare bones testing framework) that both a Windows and a Mac user needed to run (and I'm writing on Linux/Ubuntu). The Mac user complained that the fonts were too small. So I added the following:
String os = System.getProperty("os.name","generic").toLowerCase(Locale.US);
if (os.indexOf("mac") > 0) {
root.setStyle("-fx-font-size: 14pt");
}
In this case, root is the Parent
node in the Scene
instantiation. All nodes connected to root will share this font size setting, unless the setting is overwritten.
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());