How to embed .ttf fonts is JavaFx 2.2? -


firstly, quite new guy in coding. need embed font in javafxml-based app , don't know how it. have pasted font, fontname.ttf in "resources" folder in root of sources of project, ie app/src/app/resources. have set css component (text) as

#text {     -fx-font-family: url(resources/fontname.ttf); } 

i have tried adding inverted commas in url, ie url("resources/fontname.ttf");, doesn't work. have set css id component, can't problem. there other working way so? have seen http://fxexperience.com/2010/05/how-to-embed-fonts/, doesn't work since have jdk 1.7 u21. ideas correct way embed fonts?

solution approach

i updated sample javafx how display custom font in webview? demonstrate using custom true-type font in javafx controls styled using css.

key points are:

  1. place font in same location application class , ensure build system places in binary build package (e.g. application jar file).
  2. load code font in javafx code before apply style uses it. font.loadfont(customfontapp.class.getresource("tron.ttf").toexternalform(), 10);
  3. to use custom font in style class use -fx-font-family css attribute , reference name of font (e.g. in case "tron").
  4. create , load stylesheet defines style classes.
  5. apply style classes controls.

additional information

if using java 8, may interested in use web(google) fonts in javafx.

sample output using custom font

tronfonts

sample code

the example relies on tron.ttf font can download dafont.

customfontapp.java

import javafx.application.application; import javafx.geometry.pos; import javafx.scene.scene; import javafx.scene.control.label; import javafx.scene.image.*; import javafx.scene.layout.vbox; import javafx.scene.text.*; import javafx.stage.stage;  // demonstrates use of custom font. public class customfontapp extends application {   public static void main(string[] args) { launch(args); }   @override public void start(stage stage) {     stage.settitle("tron synopsis");      // load tron font.     font.loadfont(       customfontapp.class.getresource("tron.ttf").toexternalform(),        10     );      label title = new label("tron");     title.getstyleclass().add("title");      label caption = new label("a sci-fi flick set in alternate reality.");     caption.getstyleclass().add("caption");     caption.setmaxwidth(220);     caption.setwraptext(true);     caption.settextalignment(textalignment.center);      vbox layout = new vbox(10);     layout.setstyle("-fx-padding: 20px; -fx-background-color: silver");     layout.setalignment(pos.center);     layout.getchildren().setall(       title,       new imageview(         new image(           "http://ia.media-imdb.com/images/m/mv5bmty5njm2mjawov5bml5banbnxkftztywmtgymza5.v1.sy317.jpg"         )       ),       caption     );      // layout scene.     final scene scene = new scene(layout);     scene.getstylesheets().add(getclass().getresource("custom-font-styles.css").toexternalform());     stage.setscene(scene);     stage.show();   } } 

custom-font-styles.css

/** file: custom-font-styles.css  * place in same directory customfontapp.java   */  .title {     -fx-font-family: "tron";     -fx-font-size: 20; }  .caption {     -fx-font-family: "tron";     -fx-font-size: 10; } 

on fxml usage

font.loadfont(url, size) static method taking 2 parameters. don't think can invoke font.loadfont fxml , wouldn't advise if could. instead, load font in java code (as have done in answer) before load fxml or style sheet requires font.


Comments

Popular posts from this blog

Sort a complex associative array in PHP -

vb.net - How to ignore if a cell is empty nothing -

recursion - Can every recursive algorithm be improved with dynamic programming? -