How to adjust the alignments of the text in JavaFX?

JavaObject Oriented ProgrammingProgramming

You can set a fixed width for the text in user space by setting the value to the wrappingWidth property. Once you do so, given width is considered as the boundary of the text in user coordinates and, the text is arranged width in the given width.

If you haven’t given any value for this property, by default, the length longest line in the text is considered as the width of the bounding box.

Text alignment is the arrangement of the text horizontally within the bounding box. You can adjust the alignment of the text using the setTextAlignment() method. This method accepts one of the constants of the enum named TextAlignment and adjusts the text accordingly. This enum provides 3 constants −

  • CENTER − Aligns the text in the center of the bounding box.

  • JUSTIFY − Justifies the text alignment within the bounding box.

  • LEFT − Aligns the text to the left.

  • RIGHT − Aligns the text to the right.

Example

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.Scanner;
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import javafx.scene.text.Text;
import javafx.scene.text.TextAlignment;
public class TextAllignment extends Application {
   public void start(Stage stage) throws FileNotFoundException {
      //Reading the contents of a text file.
      InputStream inputStream = new FileInputStream("D:\\sample.txt");
      Scanner sc = new Scanner(inputStream);
      StringBuffer sb = new StringBuffer();
      while(sc.hasNext()) {
         sb.append(" "+sc.nextLine()+"\n");
      }
      //Creating a text object
      Text text = new Text(10.0, 25.0, sb.toString());
      //Wrapping the text
      text.setWrappingWidth(565);
      //Setting the alignment
      text.setTextAlignment(TextAlignment.Right);
      //Setting the stage
      Group root = new Group(text);
      Scene scene = new Scene(root, 595, 150, Color.BEIGE);
      stage.setTitle("Text Alignment");
      stage.setScene(scene);
      stage.show();
   }
   public static void main(String args[]){
      launch(args);
   }
}

sample.txt

Assume following are the contents of the sample.txt file −

Tutorials Point originated from the idea that there exists a class of readers who respond better 
to online content and prefer to learn new skills at their own pace from the comforts of their drawing rooms.
The journey commenced with a single tutorial on HTML in 2006 and elated by the response it generated, 
we worked our way to adding fresh tutorials to our repository which now proudly flaunts a wealth of 
tutorials and allied articles on topics ranging from programming languages to web designing to academics 
and much more.

Output

In the same way, if you change the alignment value you will get the outputs accordingly as −

LEFT −

CENTER −

JUSTIFY −

raja
Published on 14-Apr-2020 08:43:42
Advertisements