0

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?

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

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 :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.