Open In App

JavaFX | TextAlignment Class

Improve
Improve
Like Article
Like
Save
Share
Report

TextAlignment class is a part of JavaFX. The TextAlignment class represents the horizontal text alignment. TextAlignment class inherits Enum class.

Commonly Used Methods:

Method Explanation
valueOf(String n) Returns the text Alignment of specified name.
values() Returns an array which contains all the values of text alignment.

Example: Java program to create a TextFlow and add text object to it, set text Alignment and also set a combo box to change Alignment 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. Store all the names of TextAlignment values in a String array. Now create an combobox which will contain the names of TextAlignment values and also create an Action Event to handle the combobox events. The Event handler will set the TextAlignment of the TextFlow to the chosen TextAlignment 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 a combo box to change Alignment
// 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 Alignment_1 extends Application {
   
// launch the application
public void start(Stage stage)
{
   
    try {
   
        // set title for the stage
        stage.setTitle("Alignment");
   
        // create TextFlow
        TextFlow text_flow = new TextFlow();
   
        // create text
        Text text_1 = new Text("GeeksforGeeks\n");
   
        // set the text color
        text_1.setFill(Color.GREEN);
   
        // set font of the text
        text_1.setFont(Font.font("Verdana", 25));
   
        // create text
        Text text_2 = new Text("How many times were you frustrated "
                               "while looking out for a good "+
                               "collection of programming/algorithm/ "+
                               "interview questions? What did you "
                               "expect and what did you get? "
                               "This portal has been created to "
                               "provide well written, well "
                               "thought and well explained solutions "+
                                            "for selected questions.");
   
        // set the text color
        text_2.setFill(Color.BLUE);
   
        // set font of the text
        text_2.setFont(Font.font("Helvetica"
                    FontPosture.ITALIC, 15));
   
        // add text to textflow
        text_flow.getChildren().add(text_1);
        text_flow.getChildren().add(text_2);
   
        // alignment names
        String weight[] = { "CENTER", "JUSTIFY", "LEFT", "RIGHT" };
   
        // 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 alignment
                text_flow.setTextAlignment(TextAlignment.valueOf(
                                  (String)combo_box.getValue()));
            }
        };
   
        // Set on action
        combo_box.setOnAction(event);
   
        // 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/TextAlignment.html



Last Updated : 05 Nov, 2019
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads