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.

4
  • 2
    Your code worked for me. Commented Nov 5, 2017 at 14:52
  • What does System.out.println(Main.class.getResource("TRON.TTF")); display? Commented Nov 5, 2017 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 Commented Nov 5, 2017 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. Commented Nov 5, 2017 at 18:42

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.