Updated April 20, 2023
Definition of JavaFX Font
In JavaFX, font is a class that is used to denote fonts that renders the text available on screen. It is inherited from the object class.Font size is explained as mentioned in the points that are real-world measurementroughly 1/72 inch.Fonts are given to the text based on the user requirement and can be modified at any time. Some of the commonly used fonts include Verdana, Times new roman etc. In this article, we will see different aspects such as syntax, constructors, methods and examples of JavaFX font in the following sections.
Syntax of JavaFX Font
Below is the syntax of JavaFX font.
Font f = Font.font(FONT, FONT TYPE, SIZE);
- Here, FONT denotes different fonts like verdana, times new roman etc.
- FONT TYPE is bold, italics etc.
- SIZE denotes the font size.
Constructors
Following are the two constructors of JavaFX font.
- Font(double size): A font will be constructed with “system” as the default face.
- Font(Stringname, double size): A font will be constructed with mentioned size and face name.
Methods
Let us see different methods of JavaFX font.
- equals(Objectobj): This method denotes whether any of the other objects available is same as this.
- font(double s): An appropriate font will be searched based on the font name and size that is default.
- font(Stringf): An appropriate font will be searched based on the mentioned font name and size that is the default.
- font(Stringf, double s): An appropriate font will be searched based on the mentioned font name and size.
- font(Stringf, FontPosture p, double s): An appropriate font will be searched based on the mentioned family name, posture and size.
- font(Stringf, FontWeight w, double s): An appropriate font will be searched based on the mentioned family name, weight and size.
- font(Stringf, FontWeight w, FontPosture p, double s): An appropriate font will be searched based on the mentioned family name, weight, posture and size.
- getDefault(): Default font will be retrieved that is from the “System” family, “Regular “style, and consistent size with the desktop environment of the user,to the limit that can be determined.
- getFamilies(): All the families of font will be retrieved on the system of the user, including the SDK fonts as well as application fonts.
- get family(): Family of font will be retrieved.
- getFontNames(): All the names of font will be retrieved on the system of the user, including the SDK fonts as well as application fonts.
- getFontNames(Stringfamily): All the names of font will be retrieved on the system of the user that is of the mentioned family, including the SDK fonts as well as application fonts.
- getName(): Name of font will be retrieved.
- getSize(): Size of font will be retrieved.
- getStyle(): The font mentioned string explaining the style inside the font family.
- hashCode(): Hash code will be retrieved for the font object.
- loadFont(InputStreami, double s): A font resource will be loaded from the mentioned input stream.
- loadFont(StringurlStr, double size): A font resource will be loaded from the mentioned URL.
- toString(): Font object will be converted to a string representation.
Examples of JavaFX Font
Following are the examples are given below:
Example #1
Java Program that demonstrates the working of font class
Code:
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.stage.Stage;
import javafx.scene.paint.*;
import javafx.scene.text.*;
import javafx.geometry.*;
import javafx.scene.shape.*;
//main class
public class FontSample extends Application {
// application launches here
public void start(Stage st)
{
//exception handling
try {
// stage title
st.setTitle(" Sample Font ");
// TextFlow creation
TextFlow tf = new TextFlow();
// create text
Text txt = new Text("Sample \n");
// color setting of the text
txt.setFill(Color.RED);
// FONT CREATION
Font f = Font.font("Verdana", FontWeight.BOLD, 33);
// Text font setting
txt.setFont(f);
// text setting
tf.getChildren().add(txt);
//line space setting
tf.setLineSpacing(21.0f);
// VBox creation
VBox vb = new VBox(tf);
// svbox alignment setting
vb.setAlignment(Pos.CENTER);
// scene creation
Scene sc = new Scene(vb, 400, 200);
// scene setting
st.setScene(sc);
st.show();
}
//catch exception
catch (Exception ec) {
System.out.println(ec.getMessage());
}
}
// Main Method
public static void main(String args[])
{
// application launches here
launch(args);
}
}
Output:
In this program, a text of font type verdana with red color and 33 font size is getting displayed on executing the code.
Example #2
Java Program that demonstrates font class with a drop box.
Code:
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.stage.Stage;
import javafx.scene.paint.*;
import javafx.scene.text.*;
import javafx.geometry.*;
import javafx.collections.*;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
public class FontSample extends Application {
// application launches here
public void start(Stage st)
{
try {
// stage title
st.setTitle(" Sample Font ");
// TextFlow creation
TextFlow tf = new TextFlow();
// create text
Text txt = new Text("Sample \n");
// color setting of the text
txt.setFill(Color.RED);
// FONT CREATION
Font f = Font.font("Verdana", FontWeight.BOLD, 33);
// names
String w[] = { "RED", "BLACK", "PINK", "LIGHT GREEN",
"YELLOW", "BLUE", "CYAN", "ORANGE", "VIOLET",
};
ComboBox cb =
new ComboBox(FXCollections.observableArrayList(w));
// Create action event
EventHandler event =
new EventHandler() {
public void handle(ActionEvent e)
{
// set font of the text
txt.setFont(Font.font((String)cb.getValue(),
FontWeight.valueOf((String)cb.getValue()), 20));
}
};
// Create action event
EventHandler event1 =
new EventHandler() {
public void handle(ActionEvent e)
{
// set font of the text
txt.setFont(Font.font((String)cb.getValue(),
FontWeight.valueOf((String)cb.getValue()), 20));
}
};
// Set on action
cb.setOnAction(event);
// set font of the text
txt.setFont(f);
// set text
tf.getChildren().add(txt);
// set line spacing
tf.setLineSpacing(20.0f);
// create a HBox
HBox hbox = new HBox(cb);
// create VBox
VBox vbox = new VBox(hbox, tf);
// set spacing
vbox.setSpacing(30.0);
// set alignment of vbox
vbox.setAlignment(Pos.CENTER);
// create a scene
Scene sc = new Scene(vbox, 400, 300);
// set the scene
st.setScene(sc);
st.show();
}
catch (Exception ev) {
System.out.println(ev.getMessage());
}
}
// Main Method
public static void main(String args[])
{
// launch the application
launch(args);
}
}
Output:
In this program also, a text of font type verdana with red color and 33 font size is getting displayed on executing the code. The difference is that a drop down is also displayed with certain colors as options.
Recommended Articles
This is a guide to JavaFX Font. Here we also discuss the definition and syntax of JavaFX Font along with different examples and its code implementation. You may also have a look at the following articles to learn more –