JavaFX | TextAlignment Class

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
        // create TextFlow
        TextFlow text_flow = new TextFlow();
        // create text
        Text text_1 = new Text("GeeksforGeeks\n");
        // set the text color
        // 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
        // set font of the text
                    FontPosture.ITALIC, 15));
        // add text to textflow
        // 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
        // Set on action
        // set text Alignment
        // set line spacing
        // create VBox
        VBox vbox = new VBox(combo_box, text_flow);
        // set alignment of vbox
        // create a scene
        Scene scene = new Scene(vbox, 400, 300);
        // set the scene
    catch (Exception e) {
// Main Method
public static void main(String args[])
    // launch the application



Note: The above programs might not run in an online IDE please use an offline compiler.


Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using or mail your article to See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Improved By : ManasChhabra2

Article Tags :
Practice Tags :

Be the First to upvote.

Please write to us at to report any issue with the above content.