
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
How to set font to text node in JavaFX?
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 −
- Related Articles
- How to create text node in JavaFX?
- How to set text font family in HTML?
- How to set font for Text in Tkinter?
- How to add a blur effect to a text node in JavaFX?
- How to add a reflection effect to a text node in JavaFX?
- How to adjust the line spacing in the text node in JavaFX?
- How to add a drop shadow effect to a text node in JavaFX?
- How to add an inner shadow effect to a text node in JavaFX?
- How to set font for text in JTextPane with Java?
- How to rotate a node in JavaFX?
- How to set the font family for text with JavaScript?
- How to set the font size of text with JavaScript?
- How do we set text font in HTML?
- How to set a particular font for a button text in Android?
- How to set the font size of a Tkinter Canvas text item?
