This discussion is archived
7 Replies Latest reply: Apr 25, 2013 11:58 PM by jsmith RSS

Want to use -fx-font-family preference list

bvm Newbie
Currently Being Moderated
In regular CSS, the font-family attribute takes a list of family names, and uses the first one it finds that it knows about. Is there a way to accomplish the same thing in JavaFX? In particular, I hate the default monospace font, which is ugly and much fainter than other fonts of the same size. I'd like to be able to say
.foo {
    -fx-font-family: "Lucida Console", "Andale Mono", Monospace;
}
but that doesn't seem to be supported. Does anyone know of a good alternative? I could, of course, programmatically figure out which font is available, but that would seem to preclude the use of style sheets, since there isn't a way of doing dynamic substitution in .css resources, or is there?
  • 1. Re: Want to use -fx-font-family preference list
    jsmith Guru
    Currently Being Moderated
    Why not just include your preferred font with your app?
  • 2. Re: Want to use -fx-font-family preference list
    bvm Newbie
    Currently Being Moderated
    Why not just include your preferred font with your app?
    It's not a specialty font -- I just want to use one of the standard system fonts for whichever platform I'm on, and I happen to have a preference among those fonts. Why bloat my app by packing a font with it?

    (To be fair, once I pack the Java runtime with my app, I've bloated it a lot more than a little font file would. But it's the principle.)
  • 3. Re: Want to use -fx-font-family preference list
    shakir.gusaroff Expert
    Currently Being Moderated
    You can update scene styles dynamically:
    private void changeStyleSheet(String stylePath){
            this.getScene().getStylesheets().clear();
            this.getScene().getStylesheets().add(stylePath);
    I am using FontAwesome (http://fortawesome.github.com/Font-Awesome/) in my application.
    You can load custom fonts:
               Font.loadFont(getClass().getResource("font/fontawesome-webfont.ttf").toExternalForm(), 12);
    Here is the css file:
    sttyles.css
    .icons{
        -fx-font-family: FontAwesome;
        -fx-font-size: 24px;
        -fx-effect: innershadow( three-pass-box , derive(-icons-color,-70%) , 0.1em, 0.0 , 0.07em, 0.07em );
        -fx-text-fill: linear-gradient(to bottom, derive(-icons-color,20%) 10%, derive(-icons-color,-40%) 80%);
    }
  • 4. Re: Want to use -fx-font-family preference list
    David Grieve Pro
    Currently Being Moderated
    This is https://javafx-jira.kenai.com/browse/RT-8875, which, as you can see, is a very old bug. If you can come up with some code that reliably compares the desired font with the javafx.scene.Font, I might be able to use that code to fix this bug.
  • 5. Re: Want to use -fx-font-family preference list
    bvm Newbie
    Currently Being Moderated
    David Grieve wrote:
    This is https://javafx-jira.kenai.com/browse/RT-8875, which, as you can see, is a very old bug.
    I get a "Permission Violation" trying to view this ticket.
  • 6. Re: Want to use -fx-font-family preference list
    bvm Newbie
    Currently Being Moderated
    shakir.gusaroff wrote:
    You can update scene styles dynamically:
    private void changeStyleSheet(String stylePath){
    this.getScene().getStylesheets().clear();
    this.getScene().getStylesheets().add(stylePath);
    I know about the StyleSheets property, but the values have to be file names, right? When I asked about "dynamic substitution", I was questioning whether (1) you could add a stylesheet at the root defining a font with a symbol (ideally passing the stylesheet as a string, and not having to write a file into the temp directory and point at it), and then (2) refer to that symbol in descendants of that node. Sort of like stylesheets can currently refer to things like -fx-control-inner-background rather than a specific color.

    Edited by: bvm on Apr 25, 2013 6:56 PM
  • 7. Re: Want to use -fx-font-family preference list
    jsmith Guru
    Currently Being Moderated
    (1) you could add a stylesheet at the root defining a font with a symbol (ideally passing the stylesheet as a string, and not having to write a file into the temp directory and point at it), and then (2) refer to that symbol in descendants of that node. Sort of like stylesheets can currently refer to things like -fx-control-inner-background rather than a specific color.
    No, none of that suggested functionality is currently supported.

    1. There is no api to load a stylesheet from a string.

    2. The symbol based definition of items in stylesheets (like the -fx-base color) only work with paints (i.e. colors and gradients in JavaFX 2.2).

    --------

    You might want to create some new tickets to allow the symbol based definition of items stylesheets for stuff other than paints and for an api to load a stylesheet from a string. You could probably patch openjdk pretty easily to support loading of string based stylesheets.

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points