Monday 26 October 2020

JavaFX: Set font posture (italic or regular)

 'FontPosture' enum is used to set the font posture to italic. Following table summarizes the constants for font posture.

 

Font Posture

Description

REGULAR

Represents Regular.

ITALIC

Represents Italic

 

Example: Below snippet sets font style to italic.

<Text fx:id = "text" text="Learning JavaFX"  fill="lightgreen"

         style="-fx-font-weight: bold; -fx-font-size: 50; -fx-font-family: Verdana; -fx-font-style: italic"

         stroke="darkblue" strokeWidth="2"

         underline="true"/>

 

Find the below working application.

 

fontPosture.fxml 

<?import javafx.scene.*?>
<?import javafx.scene.shape.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.text.*?>

<StackPane xmlns:fx="http://javafx.com/fxml" fx:controller="com.sample.app.controller.FontPostureController">
	
	<Text fx:id = "text" text="Learning JavaFX"  fill="lightgreen"
	style="-fx-font-weight: bold; -fx-font-size: 50; -fx-font-family: Verdana; -fx-font-style: italic"
	stroke="darkblue" strokeWidth="2" 
	underline="true"/>

</StackPane>

 

FontPostureController.java

package com.sample.app.controller;

import java.net.URL;
import java.util.ResourceBundle;

import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.text.Text;

public class FontPostureController implements Initializable {

	@FXML
	private Text text;

	@Override
	public void initialize(URL location, ResourceBundle resources) {
	}

}

 

FontPostureDemo.java

package com.sample.app;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.paint.Color;
import javafx.stage.Stage;

public class FontPostureDemo extends Application {

	@Override
	public void start(Stage primaryStage) throws Exception {
		Parent root = (Parent) FXMLLoader.load(FontPostureDemo.class.getResource("/fontPosture.fxml"));

		Scene scene = new Scene(root, 600, 300, Color.WHITE);

		primaryStage.setTitle("Font Posture Demo");
		primaryStage.setScene(scene);
		primaryStage.show();

	}

	public static void main(String args[]) {
		launch(args);
	}
}

 

Output


 

 

 

Previous                                                    Next                                                    Home

No comments:

Post a Comment