Set Font globally in JavaFX
Asked Answered
L

4

25

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.

Loraineloralee answered 23/8, 2013 at 17:50 Comment(0)
U
40

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());
Unanimous answered 23/8, 2013 at 18:24 Comment(2)
Works on main window but not in sub-windowsKristy
@Kristy you need to attach the stylesheet to the Scene of the sub-windows as wellSarcocarp
B
9

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.

Blooper answered 23/8, 2013 at 18:26 Comment(0)
R
9

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.

Runofthemill answered 25/2, 2021 at 23:6 Comment(0)
L
4

Just a TornadoFX example

package bj

import tornadofx.*

class MyView : View() {
    override val root = vbox {
        button("天地玄黄")
        button("宇宙洪荒")
    }
}

class MyStylesheet : Stylesheet() {
    init {
        root {
            fontFamily = "Noto Sans CJK SC Regular"
        }
    }
}

class MyApp : App(MyView::class, MyStylesheet::class)

fun main(args: Array<String>) {
    launch<MyApp>(*args)
}
Leak answered 18/8, 2018 at 9:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.