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 text
import
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(
"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 =
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 box
import
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 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(
"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 =
     Â
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 Method
public
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