Font class is a part of JavaFX. The Font class represents fonts, which are used to render text on the screen. The size of a Font is described as being specified in points which are a real world measurement of approximately 1/72 inch. Font class inherits Object class.
Constructors of the class:
- Font(double s) : Creates a font object with specified size.
- Font(String n, double s) : Creates a font object with specified name and size.
Commonly Used Methods:
| Method | Explanation |
|---|---|
| font(double s) | Creates a font object with specified size. |
| font(String f) | Creates a font object with specified family name. |
| font(String f, double s) | Creates a font object with specified family name and size. |
| font(String f, FontPosture p, double s) | Creates a font object with specified family name, posture and size. |
| font(String f, FontWeight weight, double s) | Creates a font object with specified family name, fontweight and size. |
| font(String f, FontWeight w, FontPosture p, double s) | Creates a font object with specified family name, fontweight, font posture and size. |
| getDefault() | Returns the default font. |
| getFamilies() | Gets all the font families installed on the user’s system. |
| getFamily() | Returns the family of the font. |
| getFontNames() | Gets the names of all fonts that are installed on the users system. |
| getFontNames(String f) | Gets the names of all fonts in the specified font family that are installed on the users system. |
| loadFont(InputStream in, double s) | Loads a font resource from the specified input stream. |
| loadFont(String url, double s) | Loads a font resource from the specified URL. |
| getName() | The full font name. |
| getSize() | The point size for this font. |
Below programs illustrate the use of Font Class:
- Java Program to create a font object and apply it to a text: In this program we will create a Font named font and specify its family, its font weight and size. Apply this font to the text and add this text to the TextFlow named textflow. Create a VBox named vbox and add the textflow to the vbox and add the vbox to the scene and add the scene to stage. Call the show() function to display the results.
// Java Program to create a font objectÂ// and apply it to a textimportjavafx.application.Application;importjavafx.scene.Scene;importjavafx.scene.control.*;importjavafx.scene.layout.*;importjavafx.stage.Stage;importjavafx.scene.layout.*;importjavafx.scene.paint.*;importjavafx.scene.text.*;importjavafx.geometry.*;importjavafx.scene.layout.*;importjavafx.scene.shape.*;ÂÂpublicclassFont_1extendsApplication {   Â// launch the application   Âpublicvoidstart(Stage stage)   Â{       Âtry{           Â// set title for the stage           Âstage.setTitle("Font");           Â// create TextFlow           ÂTextFlow text_flow =newTextFlow();           Â// create text           ÂText text_1 =newText("Lazyroar\n");           Â// set the text color           Âtext_1.setFill(Color.GREEN);           Â// create a font           ÂFont font = Font.font("Verdana", FontWeight.EXTRA_BOLD,25);           Â// set font of the text           Âtext_1.setFont(font);           Â// set text           Âtext_flow.getChildren().add(text_1);           Â// set line spacing           Âtext_flow.setLineSpacing(20.0f);           Â// create VBox           ÂVBox vbox =newVBox(text_flow);           Â// set alignment of vbox           Âvbox.setAlignment(Pos.CENTER);           Â// create a scene           ÂScene scene =newScene(vbox,400,300);           Â// set the scene           Âstage.setScene(scene);           Âstage.show();       Â}       Âcatch(Exception e) {           ÂSystem.out.println(e.getMessage());       Â}   Â}   Â// Main Method   Âpublicstaticvoidmain(String args[])   Â{       Â// launch the application       Âlaunch(args);   Â}}Output:
- Java Program to create a font object and apply it to a text and allow the user to select the font from the combo box: In this program we will create a Font named font and specify its family, its font weight and size. Apply this font to the text and add this text to the TextFlow named textflow. Create a VBox named vbox and add the textflow to the vbox and add the vbox to the scene and add the scene to stage. Create two combo box and add the font names to one and the font weight to the other and create an EventHandler to handle the events of the combo boxes and set the font to the type specified by the user.
// Java Program to create a font objectÂ// and apply it to a text and allow theÂ// user to select font from the combo boximportjavafx.application.Application;importjavafx.scene.Scene;importjavafx.scene.control.*;importjavafx.scene.layout.*;importjavafx.stage.Stage;importjavafx.scene.layout.*;importjavafx.scene.paint.*;importjavafx.scene.text.*;importjavafx.geometry.*;importjavafx.scene.layout.*;importjavafx.scene.shape.*;importjavafx.collections.*;importjavafx.event.ActionEvent;importjavafx.event.EventHandler; ÂÂpublicclassFont_2extendsApplication { ÂÂ// launch the applicationpublicvoidstart(Stage stage){ÂÂtry{   Â// set title for the stage   Âstage.setTitle("Font");   Â// create TextFlow   ÂTextFlow text_flow =newTextFlow();   Â// create text   ÂText text_1 =newText("Lazyroar\n");   Â// set the text color   Âtext_1.setFill(Color.GREEN);   Â// create a font   ÂFont font = Font.font(Font.getFontNames().get(0),                         ÂFontWeight.EXTRA_BOLD,20);   Â// font weight names   ÂString weight[] = {"BLACK","BOLD",                       Â"EXTRA_BOLD",                       Â"EXTRA_LIGHT",                       Â"LIGHT",                       Â"MEDIUM",                       Â"NORMAL",                       Â"SEMI_BOLD",                       Â"THIN"};   Â// Create a combo box   ÂComboBox combo_box =     ÂnewComboBox(FXCollections.observableArrayList(weight));   Â// Create a combo box   ÂComboBox combo_box1 =     ÂnewComboBox(FXCollections.observableArrayList(Font.getFontNames()));   Â// Create action event   ÂEventHandler<ActionEvent> event =   ÂnewEventHandler<ActionEvent>() {       Âpublicvoidhandle(ActionEvent e)       Â{           Â// set font of the text           Âtext_1.setFont(Font.font((String)combo_box1.getValue(),                ÂFontWeight.valueOf((String)combo_box.getValue()),20));       Â}   Â};   Â// Create action event   ÂEventHandler<ActionEvent> event1 =      ÂnewEventHandler<ActionEvent>() {       Âpublicvoidhandle(ActionEvent e)       Â{           Â// set font of the text           Âtext_1.setFont(Font.font((String)combo_box1.getValue(),                 ÂFontWeight.valueOf((String)combo_box.getValue()),20));       Â}   Â};   Â// Set on action   Âcombo_box.setOnAction(event);   Âcombo_box1.setOnAction(event1);   Â// set font of the text   Âtext_1.setFont(font);   Â// set text   Âtext_flow.getChildren().add(text_1);   Â// set line spacing   Âtext_flow.setLineSpacing(20.0f);   Â// create a HBox   ÂHBox hbox =newHBox(combo_box, combo_box1);   Â// create VBox   ÂVBox vbox =newVBox(hbox, text_flow);   Â// set spacing   Âvbox.setSpacing(30.0);   Â// set alignment of vbox   Âvbox.setAlignment(Pos.CENTER);   Â// create a scene   ÂScene scene =newScene(vbox,400,300);   Â// set the scene   Âstage.setScene(scene);   Âstage.show();}ÂÂcatch(Exception e) {   ÂSystem.out.println(e.getMessage());}}ÂÂ// Main Methodpublicstaticvoidmain(String args[]){   Â// launch the application   Âlaunch(args);}}Output:
Note: The above programs might not run in an online IDE please use an offline compiler.
Reference: https://docs.oracle.com/javase/8/javafx/api/javafx/scene/text/Font.html

