FontWeight class is a part of JavaFX. FontWeight class defines the weight of the font. FontWeight class specifies different font weights which can be used when searching for a font on the system. FontWeight class inherits Enum class.
Commonly Used Methods:
Method | Explanation |
---|---|
findByName(String n) | Returns FontWeight by its name. |
findByWeight(int w) | Returns the closest FontWeight for a weight value. |
getWeight() | Return the visual weight. |
valueOf(String n) | Returns the enum constant of this type with the specified name. |
values() | returns all the values of the FontWeight type. |
Below programs illustrate the use of FontWeight Class:
- Java program to create a TextFlow and add text object to it, set text Alignment and also set font weight of the font of the text and set line spacing of the text flow: In this program we will create a TilePane named tile_pane. Add Label named label and some buttons to the tile_pane. Set the Alignment of the tile_pane using the setAlignment() function. Set the Font weight of the font to EXTRA_BOLD. Add the tile_pane to the scene and add the scene to the stage and call the show() function to display the final results.
// Java program to create a TextFlow and
// add text object to it, set text Alignment
// and also set font weight of the font of text
// and set line spacing of the text flow.
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
FontWeight_1
extends
Application {
Â
ÂÂ Â Â Â
// launch the application
   Â
public
void
start(Stage stage)
   Â
{
Â
ÂÂ Â Â Â Â Â Â Â
try
{
Â
ÂÂ Â Â Â Â Â Â Â Â Â Â Â
// set title for the stage
           Â
stage.setTitle(
"FontWeight"
);
Â
ÂÂ Â Â Â Â Â Â Â Â Â Â Â
// 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);
Â
ÂÂ Â Â Â Â Â Â Â Â Â Â Â
// set font of the text
           Â
text_1.setFont(Font.font(
"Verdana"
, FontWeight.EXTRA_BOLD,
25
));
Â
ÂÂ Â Â Â Â Â Â Â Â Â Â Â
// set text
           Â
text_flow.getChildren().add(text_1);
Â
ÂÂ Â Â Â Â Â Â Â Â Â Â Â
// set text Alignment
           Â
text_flow.setTextAlignment(TextAlignment.CENTER);
Â
ÂÂ Â Â Â Â Â Â Â Â Â Â Â
// 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 TextFlow and add text object to it, set text Alignment and also set font weight of the font of the text and also set a combo box to change font weight and set line spacing of the text flow: In this program we will create a TilePane named tile_pane. Add Label named label and some buttons to the tile_pane. Set the Alignment of the tile_pane using the setAlignment() function. Now set the FontWeight of the font to EXTRA_BOLD. Store all the names of FontWeight values in a String array. Now create a combobox which will contain the names of FontWeight values and also create an Action Event to handle the combobox events. The Event handler will set the font weight of the font to the chosen FontWeight value. Now create a VBox and add the tilepane and the combo box to vbox. Finally, add the vbox to the scene and add the scene to the stage and call the show() function to display the final results.
// Java program to create a TextFlow and
// add text object to it, set text Alignment
// and also set font weight of the font of text
// and also set a combo box to change font weight
// and set line spacing of the text flow.
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
FontWeight_2
extends
Application {
Â
Â// launch the application
public
void
start(Stage stage)
{
Â
ÂÂ Â Â Â
try
{
Â
ÂÂ Â Â Â Â Â Â Â
// set title for the stage
       Â
stage.setTitle(
"FontWeight"
);
Â
ÂÂ Â Â Â Â Â Â Â
// 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);
Â
ÂÂ Â Â Â Â Â Â Â
// set font of the text
       Â
text_1.setFont(Font.font(Font.getFontNames().get(
0
),
                               Â
FontWeight.EXTRA_BOLD,
50
));
Â
ÂÂ Â Â Â Â Â Â Â
// 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 action event
       Â
EventHandler<ActionEvent> event =
new
EventHandler<ActionEvent>() {
Â
ÂÂ Â Â Â Â Â Â Â Â Â Â Â
public
void
handle(ActionEvent e)
           Â
{
                Â
ÂÂ Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â
// set font of the text
               Â
text_1.setFont(Font.font(Font.getFontNames().get(
0
),Â
                Â
FontWeight.valueOf((String)combo_box.getValue()),
50
));
           Â
}
       Â
};
Â
ÂÂ Â Â Â Â Â Â Â
// Set on action
       Â
combo_box.setOnAction(event);
Â
ÂÂ Â Â Â Â Â Â Â
// set text
       Â
text_flow.getChildren().add(text_1);
Â
ÂÂ Â Â Â Â Â Â Â
// set text Alignment
       Â
text_flow.setTextAlignment(TextAlignment.CENTER);
Â
ÂÂ Â Â Â Â Â Â Â
// set line spacing
       Â
text_flow.setLineSpacing(
20
.0f);
Â
ÂÂ Â Â Â Â Â Â Â
// create VBox
       Â
VBox vbox =
new
VBox(combo_box, 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:
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/FontWeight.html