JavaFX | Font Class

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 applicationpublicvoidstart(Stage stage){try{// set title for the stagestage.setTitle("Font");// create TextFlowTextFlow text_flow =newTextFlow();// create textText text_1 =newText("Lazyroar\n");// set the text colortext_1.setFill(Color.GREEN);// create a fontFont font = Font.font("Verdana", FontWeight.EXTRA_BOLD,25);// set font of the texttext_1.setFont(font);// set texttext_flow.getChildren().add(text_1);// set line spacingtext_flow.setLineSpacing(20.0f);// create VBoxVBox vbox =newVBox(text_flow);// set alignment of vboxvbox.setAlignment(Pos.CENTER);// create a sceneScene scene =newScene(vbox,400,300);// set the scenestage.setScene(scene);stage.show();}catch(Exception e) {System.out.println(e.getMessage());}}// Main Methodpublicstaticvoidmain(String args[]){// launch the applicationlaunch(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 stagestage.setTitle("Font");// create TextFlowTextFlow text_flow =newTextFlow();// create textText text_1 =newText("Lazyroar\n");// set the text colortext_1.setFill(Color.GREEN);// create a fontFont font = Font.font(Font.getFontNames().get(0),FontWeight.EXTRA_BOLD,20);// font weight namesString weight[] = {"BLACK","BOLD","EXTRA_BOLD","EXTRA_LIGHT","LIGHT","MEDIUM","NORMAL","SEMI_BOLD","THIN"};// Create a combo boxComboBox combo_box =newComboBox(FXCollections.observableArrayList(weight));// Create a combo boxComboBox combo_box1 =newComboBox(FXCollections.observableArrayList(Font.getFontNames()));// Create action eventEventHandler<ActionEvent> event =newEventHandler<ActionEvent>() {publicvoidhandle(ActionEvent e){// set font of the texttext_1.setFont(Font.font((String)combo_box1.getValue(),FontWeight.valueOf((String)combo_box.getValue()),20));}};// Create action eventEventHandler<ActionEvent> event1 =newEventHandler<ActionEvent>() {publicvoidhandle(ActionEvent e){// set font of the texttext_1.setFont(Font.font((String)combo_box1.getValue(),FontWeight.valueOf((String)combo_box.getValue()),20));}};// Set on actioncombo_box.setOnAction(event);combo_box1.setOnAction(event1);// set font of the texttext_1.setFont(font);// set texttext_flow.getChildren().add(text_1);// set line spacingtext_flow.setLineSpacing(20.0f);// create a HBoxHBox hbox =newHBox(combo_box, combo_box1);// create VBoxVBox vbox =newVBox(hbox, text_flow);// set spacingvbox.setSpacing(30.0);// set alignment of vboxvbox.setAlignment(Pos.CENTER);// create a sceneScene scene =newScene(vbox,400,300);// set the scenestage.setScene(scene);stage.show();}catch(Exception e) {System.out.println(e.getMessage());}}// Main Methodpublicstaticvoidmain(String args[]){// launch the applicationlaunch(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




