How to add custom fonts to a text in JavaFX?

You can set the desired font to the text node in JavaFX using the setFont() method. This method accepts an object of the class javafx.scene.text.Font.

The Font class represents the fonts in JavaFX, this class provides several variants of a method named font().as shown below −

font(double size)
font(String family)
font(String family, double size)
font(String family, FontPosture posture, double size)
font(String family, FontWeight weight, double size)
font(String family, FontWeight weight, FontPosture posture, double size)

All these methods are static and returns a Font object. To set a font you need to create the font object using one of these methods and set the created front to the text using the setText() method.

Creating custom font

In addition to these you can also load your own fonts using the loadFont() method.This method has two variants where

  • One is method accepts an object of the InputStream class, representing the required font and a double variable representing the size, as parameters.

  • One method accepts a String variable representing the URL of the font and a double variable representing the size, as parameters.

To load a custom font −

  • Create a directory as resources/fonts in your project folder.

  • Download the required font and place the file in the above created folder.

  • Load the font using the loadFont() method.

  • Create a text node by instantiating the javafx.scene.text.Text class.

  • Set the font to the text using the setText() method.


import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.effect.DropShadow;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
public class CustomFontExample extends Application {
   public void start(Stage stage) throws FileNotFoundException {
      //Creating a text object
      Text text = new Text(30.0, 75.0, "ట్యూ టోరియల్స్ పాయింట్ కి స్వా గతిం");
      //Loading a font from local file system
      Font font = Font.loadFont("file:resources/fonts/TenaliRamakrishna-Regular.ttf", 45);
      //Setting the font
      //Setting color of the text
      //Creating the inner shadow effect
      //Creating the drop shadow effect
      DropShadow shadow = new DropShadow();
      //Setting the effect to the text
      //Setting the stage
      Group root = new Group(text);
      Scene scene = new Scene(root, 595, 150, Color.BEIGE);
      stage.setTitle("Custom Font");
   public static void main(String args[]){


Updated on: 16-May-2020

5K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started