11

I'm using a CSS style sheet with a style class like this:

.sublabel {
    -fx-font-style: italic; 
    -fx-font-size: 12;
}

...which has been set as the Label's Style Class in Scene Builder. The font size changes as expected (smaller, since I have -fx-font-size: 14; under the .root class but this is the only font-related setting). Bold style works fine, but it refuses to use an italic font. I'm not using a custom font anywhere, so this should be using the default font JavaFX 8 uses on Win7. I've also tried setting it under Style independently.

What could cause the style request to be ignored?

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Manius
  • 3,594
  • 3
  • 34
  • 44
  • You should add the accompanying java code. `.sublabel` is a class, is this what you intended? Did you apply the class correctly to the label? – Parth Mehrotra Aug 24 '19 at 00:53
  • I would imagine I did at the time, since I said above that "bold style works fine" (if you change italic to bold). Doubt there was any code to show, it was done in Scene Builder. – Manius May 11 '20 at 23:25

1 Answers1

2

The default font in JavaFX, "system", does not support italic. You can even choose italic in SceneBuilder, but it will only work for fonts that support it.

Try using a TrueType font, e.g. Verdana, then it should work without problems.

Either use the style version below, or the more direct: Font name="Verdana Italic"

<Button style="-fx-font-style: italic;" text="Button">
    <font>
        <Font name="Verdana"/>
    </font>
</Button>
jepml
  • 56
  • 4
  • 1
    Makes sense. But now I'm going to throw another wrench in. I just tried replicating this on Linux (Mint) without setting any font (it was just "system"). Italics worked fine! Maybe it's some OS level difference... – Manius Feb 02 '23 at 00:55
  • 1
    Of course this is also with Java 17, not sure what version I used back in 2018. – Manius Feb 02 '23 at 05:54