JavaFX | FontWeight Class

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.importjavafx.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.*;publicclassFontWeight_1extendsApplication {// launch the applicationpublicvoidstart(Stage stage){try{// set title for the stagestage.setTitle("FontWeight");// create TextFlowTextFlow text_flow =newTextFlow();// create textText text_1 =newText("Lazyroar\n");// set the text colortext_1.setFill(Color.GREEN);// set font of the texttext_1.setFont(Font.font("Verdana", FontWeight.EXTRA_BOLD,25));// set texttext_flow.getChildren().add(text_1);// set text Alignmenttext_flow.setTextAlignment(TextAlignment.CENTER);// 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 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.importjavafx.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;publicclassFontWeight_2extendsApplication {// launch the applicationpublicvoidstart(Stage stage){try{// set title for the stagestage.setTitle("FontWeight");// create TextFlowTextFlow text_flow =newTextFlow();// create textText text_1 =newText("Lazyroar\n");// set the text colortext_1.setFill(Color.GREEN);// set font of the texttext_1.setFont(Font.font(Font.getFontNames().get(0),FontWeight.EXTRA_BOLD,50));// 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 action eventEventHandler<ActionEvent> event =newEventHandler<ActionEvent>() {publicvoidhandle(ActionEvent e){// set font of the texttext_1.setFont(Font.font(Font.getFontNames().get(0),FontWeight.valueOf((String)combo_box.getValue()),50));}};// Set on actioncombo_box.setOnAction(event);// set texttext_flow.getChildren().add(text_1);// set text Alignmenttext_flow.setTextAlignment(TextAlignment.CENTER);// set line spacingtext_flow.setLineSpacing(20.0f);// create VBoxVBox vbox =newVBox(combo_box, 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:
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




