
- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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.
Example
import java.io.FileNotFoundException; 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 text.setFont(font); //Setting color of the text text.setFill(Color.BROWN); text.setStroke(Color.BLUEVIOLET); text.setStrokeWidth(0.5); //Creating the inner shadow effect //Creating the drop shadow effect DropShadow shadow = new DropShadow(); shadow.setOffsetY(5.0); //Setting the effect to the text text.setEffect(shadow); //Setting the stage Group root = new Group(text); Scene scene = new Scene(root, 595, 150, Color.BEIGE); stage.setTitle("Custom Font"); stage.setScene(scene); stage.show(); } public static void main(String args[]){ launch(args); } }
Output
- Related Questions & Answers
- How to add various fonts to text using text flow in JavaFX?
- 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 add stroke and color to text in JavaFX?
- How to add a combination of multiple effects to text in JavaFX?
- How to add a drop shadow effect to a text node in JavaFX?
- How to add LCD (liquid crystal display) to text in JavaFX?
- How to add an inner shadow effect to a text node in JavaFX?
- How to create a text area in JavaFX?
- How to add mnemonics to a menu in JavaFX?
- How to add data to a TableView in JavaFX?
- How to add a separator to a Menu in JavaFX?
- How to create a text field using JavaFX?
- How to create a text flow layout in JavaFX?
- How to create text node in JavaFX?