When I set a custom font for a Label in JavaFX 2, it works. But when I change -fx-font-size or other font-properties in the CSS stylesheet, the default font is used instead of my custom font. Is there a way to set the font size and color while maintaining my custom font? Or is it possible to set a custom font for my label in the CSS stylesheet?
Add a comment
|
1 Answer
First, to be sure check your custom font is in available/loadable list by Font.getFontNames().
Then check its usage by either:
label.setStyle(
"-fx-font-family: MyFont;"
+ "-fx-font-size: 32;"
+ "-fx-font-style: italic;"
+ "-fx-text-fill: blue");
or
label.setFont(Font.font("MyFont", FontPosture.ITALIC, 32));
label.setTextFill(Color.BLUE);
These 2 are equivalent. Additionally you can carry out the style attributes to CSS file. Changing the font attributes other than -fx-font-family should not affect the font family attribute.
1 Comment
brunob
Thank you! I'm not sure why it didn't work before when I tried this in a similar way in my CSS stylesheet, but maybe it's because I typed
-fx-font-family: Gill Sans MT instead of -fx-font-family: 'Gill Sans MT' (name of the font between single quotes). Anyway, it seems to work now :)