0

I was following the instructions given in the correct answer in this post.

Javafx How to display custom font in webview?

The font file is located at the same package as the Main class. enter image description here

This is my full code.

package sample;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.StackPane;
import javafx.scene.text.Font;
import javafx.stage.Stage;

public class Main extends Application{
    public void start(Stage primaryStage) throws Exception{
        Font.loadFont(Main.class.getResource("TRON.TTF").toExternalForm(),10);

       StackPane pane = new StackPane();
       Label label = new Label("Text");

       pane.getChildren().add(label);
       label.setStyle("-fx-font-family:'TRON'; -fx-font-size: 50px;-fx-text-fill: red");

       Scene scene = new Scene(pane,300,300);
       primaryStage.setScene(scene);
       primaryStage.show();
    }


    public static void main(String[] args) {
        launch(args);
    }
}

Output

enter image description here

The font color and the font size has been affected. But the Tron font family has not been used.

I downloaded the font ttf file from here.

Enzio
  • 799
  • 13
  • 32
  • 2
    Your code worked for me. – SedJ601 Nov 05 '17 at 14:52
  • What does `System.out.println(Main.class.getResource("TRON.TTF"));` display? – VGR Nov 05 '17 at 15:53
  • @VGR It displays this file:/C:/Users/prave_000/OneDrive/Programs/Java/OOP%20-%20Y2/04%20Practice/JavaFx%20Tutorials/out/production/JavaFx%20Tutorials/sample/TRON.TTF – Enzio Nov 05 '17 at 16:05
  • OneDrive could be your problem. Just a guess. Try moving the project to the `NetBeans`(assuming here) project folder or a local folder under `MyDocuments`. – SedJ601 Nov 05 '17 at 18:42

0 Answers0