Monday, February 25, 2013

Load True Type Font (ttf) in JavaFX

To load True Type Font (ttf) in JavaFX, create a folder fonts under your Source Packages, copy your ttf file into it. Load the font with Font.loadFont() method.

Save ttf file in /fonts folder.
Save ttf file in /fonts folder.

package javafxttf;

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.scene.text.Font;
import javafx.stage.Stage;

 * @web
public class JavaFXTTF extends Application {
    public void start(Stage primaryStage) {
        Font myFontloadFontAirstreamNF20 = 
                .getResourceAsStream("/fonts/AirstreamNF.ttf"), 20);

        Button btn = new Button();
        btn.setText("Say 'Hello World'");
        btn.setOnAction(new EventHandler<ActionEvent>() {
            public void handle(ActionEvent event) {
                System.out.println("Hello World!");
        StackPane root = new StackPane();
        Scene scene = new Scene(root, 300, 250);
        primaryStage.setTitle("Hello World!");

    public static void main(String[] args) {

Load True Type Font (ttf) in JavaFX
Load True Type Font (ttf) in JavaFX

Related article: Apply system installed fonts.

No comments:

Post a Comment