JavaFX | Font Class - GeeksforGeeks

18 Sep, 2018
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.
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:
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 textimport javafx.application.Application;import javafx.scene.Scene;import javafx.scene.control.*;import javafx.scene.layout.*;import javafx.stage.Stage;import javafx.scene.layout.*;import javafx.scene.paint.*;import javafx.scene.text.*;import javafx.geometry.*;import javafx.scene.layout.*;import javafx.scene.shape.*;  public class Font_1 extends Application {      // launch the application    public void start(Stage stage)    {          try {              // set title for the stage            stage.setTitle("Font");              // create TextFlow            TextFlow text_flow = new TextFlow();              // create text            Text text_1 = new Text("GeeksforGeeks\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 = new VBox(text_flow);              // set alignment of vbox            vbox.setAlignment(Pos.CENTER);              // create a scene            Scene scene = new Scene(vbox, 400, 300);              // set the scene            stage.setScene(scene);              stage.show();        }          catch (Exception e) {              System.out.println(e.getMessage());        }    }      // Main Method    public static void main(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 boximport javafx.application.Application;import javafx.scene.Scene;import javafx.scene.control.*;import javafx.scene.layout.*;import javafx.stage.Stage;import javafx.scene.layout.*;import javafx.scene.paint.*;import javafx.scene.text.*;import javafx.geometry.*;import javafx.scene.layout.*;import javafx.scene.shape.*;import javafx.collections.*;import javafx.event.ActionEvent;import javafx.event.EventHandler;   public class Font_2 extends Application {   // launch the applicationpublic void start(Stage stage){  try {      // set title for the stage    stage.setTitle("Font");      // create TextFlow    TextFlow text_flow = new TextFlow();      // create text    Text text_1 = new Text("GeeksforGeeks\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 =      new ComboBox(FXCollections.observableArrayList(weight));      // Create a combo box    ComboBox combo_box1 =       new ComboBox(FXCollections.observableArrayList(Font.getFontNames()));      // Create action event    EventHandler<ActionEvent> event =    new EventHandler<ActionEvent>() {          public void handle(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 =        new EventHandler<ActionEvent>() {          public void handle(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 = new HBox(combo_box, combo_box1);      // create VBox    VBox vbox = new VBox(hbox, text_flow);      // set spacing    vbox.setSpacing(30.0);      // set alignment of vbox    vbox.setAlignment(Pos.CENTER);      // create a scene    Scene scene = new Scene(vbox, 400, 300);      // set the scene    stage.setScene(scene);      stage.show();}  catch (Exception e) {      System.out.println(e.getMessage());}}  // Main Methodpublic static void main(String args[]){      // launch the application    launch(args);}}Output:
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 textimport javafx.application.Application;import javafx.scene.Scene;import javafx.scene.control.*;import javafx.scene.layout.*;import javafx.stage.Stage;import javafx.scene.layout.*;import javafx.scene.paint.*;import javafx.scene.text.*;import javafx.geometry.*;import javafx.scene.layout.*;import javafx.scene.shape.*;  public class Font_1 extends Application {      // launch the application    public void start(Stage stage)    {          try {              // set title for the stage            stage.setTitle("Font");              // create TextFlow            TextFlow text_flow = new TextFlow();              // create text            Text text_1 = new Text("GeeksforGeeks\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 = new VBox(text_flow);              // set alignment of vbox            vbox.setAlignment(Pos.CENTER);              // create a scene            Scene scene = new Scene(vbox, 400, 300);              // set the scene            stage.setScene(scene);              stage.show();        }          catch (Exception e) {              System.out.println(e.getMessage());        }    }      // Main Method    public static void main(String args[])    {          // launch the application        launch(args);    }}Output:
// Java Program to create a font object // and apply it to a textimport javafx.application.Application;import javafx.scene.Scene;import javafx.scene.control.*;import javafx.scene.layout.*;import javafx.stage.Stage;import javafx.scene.layout.*;import javafx.scene.paint.*;import javafx.scene.text.*;import javafx.geometry.*;import javafx.scene.layout.*;import javafx.scene.shape.*;  public class Font_1 extends Application {      // launch the application    public void start(Stage stage)    {          try {              // set title for the stage            stage.setTitle("Font");              // create TextFlow            TextFlow text_flow = new TextFlow();              // create text            Text text_1 = new Text("GeeksforGeeks\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 = new VBox(text_flow);              // set alignment of vbox            vbox.setAlignment(Pos.CENTER);              // create a scene            Scene scene = new Scene(vbox, 400, 300);              // set the scene            stage.setScene(scene);              stage.show();        }          catch (Exception e) {              System.out.println(e.getMessage());        }    }      // Main Method    public static void main(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 boximport javafx.application.Application;import javafx.scene.Scene;import javafx.scene.control.*;import javafx.scene.layout.*;import javafx.stage.Stage;import javafx.scene.layout.*;import javafx.scene.paint.*;import javafx.scene.text.*;import javafx.geometry.*;import javafx.scene.layout.*;import javafx.scene.shape.*;import javafx.collections.*;import javafx.event.ActionEvent;import javafx.event.EventHandler;   public class Font_2 extends Application {   // launch the applicationpublic void start(Stage stage){  try {      // set title for the stage    stage.setTitle("Font");      // create TextFlow    TextFlow text_flow = new TextFlow();      // create text    Text text_1 = new Text("GeeksforGeeks\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 =      new ComboBox(FXCollections.observableArrayList(weight));      // Create a combo box    ComboBox combo_box1 =       new ComboBox(FXCollections.observableArrayList(Font.getFontNames()));      // Create action event    EventHandler<ActionEvent> event =    new EventHandler<ActionEvent>() {          public void handle(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 =        new EventHandler<ActionEvent>() {          public void handle(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 = new HBox(combo_box, combo_box1);      // create VBox    VBox vbox = new VBox(hbox, text_flow);      // set spacing    vbox.setSpacing(30.0);      // set alignment of vbox    vbox.setAlignment(Pos.CENTER);      // create a scene    Scene scene = new Scene(vbox, 400, 300);      // set the scene    stage.setScene(scene);      stage.show();}  catch (Exception e) {      System.out.println(e.getMessage());}}  // Main Methodpublic static void main(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 font from the combo boximport javafx.application.Application;import javafx.scene.Scene;import javafx.scene.control.*;import javafx.scene.layout.*;import javafx.stage.Stage;import javafx.scene.layout.*;import javafx.scene.paint.*;import javafx.scene.text.*;import javafx.geometry.*;import javafx.scene.layout.*;import javafx.scene.shape.*;import javafx.collections.*;import javafx.event.ActionEvent;import javafx.event.EventHandler;   public class Font_2 extends Application {   // launch the applicationpublic void start(Stage stage){  try {      // set title for the stage    stage.setTitle("Font");      // create TextFlow    TextFlow text_flow = new TextFlow();      // create text    Text text_1 = new Text("GeeksforGeeks\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 =      new ComboBox(FXCollections.observableArrayList(weight));      // Create a combo box    ComboBox combo_box1 =       new ComboBox(FXCollections.observableArrayList(Font.getFontNames()));      // Create action event    EventHandler<ActionEvent> event =    new EventHandler<ActionEvent>() {          public void handle(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 =        new EventHandler<ActionEvent>() {          public void handle(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 = new HBox(combo_box, combo_box1);      // create VBox    VBox vbox = new VBox(hbox, text_flow);      // set spacing    vbox.setSpacing(30.0);      // set alignment of vbox    vbox.setAlignment(Pos.CENTER);      // create a scene    Scene scene = new Scene(vbox, 400, 300);      // set the scene    stage.setScene(scene);      stage.show();}  catch (Exception e) {      System.out.println(e.getMessage());}}  // Main Methodpublic static void main(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
JavaFX
Java
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Initialize an ArrayList in Java
HashMap in Java with Examples
Interfaces in Java
Object Oriented Programming (OOPs) Concept in Java
How to iterate any Map in Java
Multidimensional Arrays in Java
Stack Class in Java
Overriding in Java
Set in Java
Collections in Java