How to set font to text node in JavaFX?

JavaObject Oriented ProgrammingProgramming

In JavaFX, the text node is represented by the javafx.scene.text.Text class. By default, the text created by JavaFX will be as follows −

Setting the desired font to the text node

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)

Where,

  • size (double) represents the size of the font.

  • family (string) represents the family of the font that we want to apply to the text. You can get the names of installed font families using the getFamilies() method.

  • weight represents the weight of the font (one of the constants of the FontWeight Enum: BLACK, BOLD, EXTRA_BOLD, EXTRA_LIGHT, LIGHT, MEDIUM, NORMAL, SEMI_BOLD, THIN).

  • posture represents the font posture (one of the constants of the FontPosture Enum: REGULAR, ITALIC).

All these methods are static and return a Font object. Therefore, to set a font to the text node −

  • Instantiate the Text class.

  • Set the basic properties like position and text string, using the setter methods or, bypassing them as arguments to the constructor.

  • Create the Font object using one of the font() methods.

  • Set the created font to the text using the setFont() method.

  • Add the created node to the Group object.

Example

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.Scanner;
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import javafx.scene.text.Font;
import javafx.scene.text.FontPosture;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;
public class SettingFont extends Application {
   public void start(Stage stage) throws FileNotFoundException {
      //Reading the contents of a text file.
      InputStream inputStream = new FileInputStream("D:\\sample.txt");
      Scanner sc = new Scanner(inputStream);
      StringBuffer sb = new StringBuffer();
      while(sc.hasNext()) {
         sb.append(" "+sc.nextLine()+"\n");
      }
      String str = sb.toString();
      //Creating a text object
      Text text = new Text();
      //Setting the basic properties of text
      text.setText(str);
      text.setX(10.0);
      text.setY(25.0);
      text.setWrappingWidth(580);
      //Creating the font object
      String font_name = Font.getFamilies().get(25);
      System.out.println("Font Name:"+font_name);
      int size = 25;
      Font font = Font.font(font_name, FontWeight.BOLD, FontPosture.REGULAR, size);
      //Setting font to the text
      text.setFont(font);
      //Setting the stage
      Group root = new Group(text);
      Scene scene = new Scene(root, 595, 300, Color.BEIGE);
      stage.setTitle("Displaying Text");
      stage.setScene(scene);
      stage.show();
   }
   public static void main(String args[]){
      launch(args);
   }
}

sample.txt

Assume following are the contents of the sample.txt file −

JavaFX is a Java library used to build Rich Internet Applications. The applications written using this library can run consistently across multiple platforms. The applications developed using JavaFX can run on various devices such as Desktop Computers, Mobile Phones, TVs, Tablets, etc..
To develop GUI Applications using Java programming language, the programmers rely on libraries such as Advanced Windowing Tool kit and Swing. After the advent of JavaFX, these Java programmers can now develop GUI applications effectively with rich content.

Output

Font Name: Brush Script MT

It also generates the following window −

raja
Published on 14-Apr-2020 08:17:15
Advertisements