How to set custom fonts in JavaFX Scene Builder using CSS


Tags: css,fonts,javafx,scenebuilder

Problem :

I'm making a GUI in JavaFX Scene Builder and would like all text (Labels, Text Fields, Comboboxes) to use the same font. The problem is it's a custom font that likely won't be on every client's computer.

I've tried CSS:

@font-face {
   font-family: DIN;
   src: url(DIN.tff);
}

.text {
   -fx-font-family: DIN;
   -fx-font-style: bold;
}

Saved the above code to file Font.css and tried applying it to each GUI element through Scene Builder's JavaFX CSS fields, but it's not working.

Have I made a mistake in the CSS? Or is there a better way to go about this? I'd prefer not to have to load the font and set it for every element in the Java code itself if I don't have to.



Solution :

Make sure to use Font.loadFont to actually load the font on application startup. Then you should be able to use the font from CSS. Be careful to use the font's name, not the font file's name. That's a common mistake.

I have used the following before to load and use a custom font:

Font.loadFont(getClass().getResourceAsStream("/resources/fonts/marck.ttf"), 14);

and:

-fx-font-family: "Marck Script";

FYI: the quotes are only needed if the name contains a space.


    CSS Howto..

    How to add animation to a bar graph with jQuery?

    How to check if css value is supported by the browser?

    How do JSF/PrimeFaces tags get resolved to default CSS styles?

    TD Borders from a table, not showing in IE8 [duplicate]

    How to make a div align with a specific td on the page?

    How can I retrieve infos from PHP DOMElement?

    Updating CSS via plain JavaScript… how do I update if the property uses vendor prefixes?

    CSS and Javascript too tightly coupled (CSS is not just presentation but is used by UI and JS), how to improve this?

    How to implement CSS sprite background into existing Javascript variable?

    How to override css grouped styles

    Make converted joomla template show 2 colums in main content

    Using CSS, how can I style a div tag to full the width, even when the viewport is scaled?

    How do I make text and other items look like chalk without a specialized font using only HTML and CSS? [closed]

    How can I center vertically a div in bootstrap?

    How to make a content div stretch from a header div to a footer div?

    CSS: How to set hover effect on menu-item when submenu-item is displayed

    How to apply CSS Style to QMenu via an entire MainWIndow

    How to define multiple CSS attributes in JQuery?

    How do I get this dropdown to be on top of the other elements

    How do I update an div css to display an complete image when section is complete?

    How to make each element to be fully independend with CSS?

    How can I align an image to the center in a Bootstrap column?

    How to display child elements of
  • at 100% height?
  • Do East Asian (e.g. Chinese) characters have baselines? How do they align with English in CSS?

    How do I make my CSS link up to files in different directories

    How do I animate background image size on hover smoothly in css?

    How to layout follow content using CSS?

    How can I change my code from PHP to HTML?

    How to override\edit Zurb Foundation base CSS styles?

    How to CENTER a Vertical Menu for ALL Screen Resolutions?