JavaFX Tutorial - JavaFX Text








Another basic JavaFX node is the Text node which allow us to display test on the scene graph.

To create Text nodes, use the javafx.scene.text.Text class.

All JavaFX scene nodes extend from javafx.scene.Node and they inherit many capabilities such as the ability to scale, translate, or rotate.

The Text node's direct parent is a javafx.scene.shape.Shape class. We can perform geometric operation between two Text such as subtract, intersect, or union. You can also clip viewport areas with a Text.

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.paint.Color;
import javafx.scene.text.Text;
import javafx.stage.Stage;
//w  w w.java2  s  .c  o m
public class Main extends Application {
    public static void main(String[] args) {
        Application.launch(args);
    }

    @Override
    public void start(Stage primaryStage) {
        primaryStage.setTitle("Drawing Text");
        Group root = new Group();
        Scene scene = new Scene(root, 300, 250, Color.WHITE);
        int x = 100;
        int y = 100;
        int red = 30;
        int green = 40;
        int blue = 50;

        Text text = new Text(x, y, "JavaFX 2.0");

        text.setFill(Color.rgb(red, green, blue, .99));
        text.setRotate(60);
        root.getChildren().add(text);
        

        primaryStage.setScene(scene);
        primaryStage.show();
    }
}

The code above generates the following result.

null




Rotate a Text

Rotate a Text

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.paint.Color;
import javafx.scene.text.Text;
import javafx.stage.Stage;
/*from w  ww  . j a v a 2 s. c om*/
public class Main extends Application {
    public static void main(String[] args) {
        Application.launch(args);
    }

    @Override
    public void start(Stage primaryStage) {
        primaryStage.setTitle("Drawing Text");
        Group root = new Group();
        Scene scene = new Scene(root, 300, 250, Color.WHITE);
        int x = 100;
        int y = 100;
        int red = 30;
        int green = 40;
        int blue = 50;

        Text text = new Text(x, y, "JavaFX 2.0");

        text.setFill(Color.rgb(red, green, blue, .99));
        text.setRotate(60);
        root.getChildren().add(text);
        

        primaryStage.setScene(scene);
        primaryStage.show();
    }
}

The code above generates the following result.

null




Text Fonts

JavaFX's Font API enables you to change font styles and font sizes.

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.effect.DropShadow;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;
import javafx.stage.Stage;
//from   www . j  a v  a  2s.c o m
public class Main extends Application {
  public static void main(String[] args) {
    Application.launch(args);
  }

  @Override
  public void start(Stage primaryStage) {
    primaryStage.setTitle("");
    Group root = new Group();
    Scene scene = new Scene(root, 300, 250, Color.WHITE);

    Group g = new Group();


    Text t = new Text();
    t.setCache(true);
    t.setX(10.0);
    t.setY(70.0);
    t.setFill(Color.RED);
    t.setText("JavaFX");
    t.setFont(Font.font(null, FontWeight.BOLD, 32));
    g.getChildren().add(t);

    

    root.getChildren().add(g);
    primaryStage.setScene(scene);
    primaryStage.show();
  }
}

The code above generates the following result.

null

Example

Text with CHOCOLATE color and Font.SERIF

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import javafx.stage.Stage;
//from   w  w w . ja va2 s  . c  o m
public class Main extends Application {

    public static void main(String[] args) {
        Application.launch(args);
    }
    
    @Override
    public void start(Stage primaryStage) {
        primaryStage.setTitle("Title");
        
        final Circle circ = new Circle(40, 40, 30);
        final Group root = new Group(circ);
        
        final Scene scene = new Scene(root, 800, 400, Color.BEIGE);

        final Text text1 = new Text(25, 25, "java2s.com");
        text1.setFill(Color.CHOCOLATE);
        text1.setFont(Font.font(java.awt.Font.SERIF, 25));
        root.getChildren().add(text1);
        
        primaryStage.setScene(scene);
        primaryStage.show();
    }
}

The code above generates the following result.

null

Text Effects

The DropShadow object is positioned based on an x, y offset in relation to the Text node. You can set the color of the shadow.

The following code shows how to draw Text with DropShadow.

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.effect.DropShadow;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;
import javafx.stage.Stage;
//  www  . j av  a 2s  .  c  o m
public class Main extends Application {
  public static void main(String[] args) {
    Application.launch(args);
  }

  @Override
  public void start(Stage primaryStage) {
    primaryStage.setTitle("");
    Group root = new Group();
    Scene scene = new Scene(root, 300, 250, Color.WHITE);

    Group g = new Group();

    DropShadow ds = new DropShadow();
    ds.setOffsetY(3.0);
    ds.setColor(Color.color(0.4, 0.4, 0.4));

    Text t = new Text();
    t.setEffect(ds);
    t.setCache(true);
    t.setX(10.0);
    t.setY(70.0);
    t.setFill(Color.RED);
    t.setText("JavaFX drop shadow...");
    t.setFont(Font.font(null, FontWeight.BOLD, 32));
    g.getChildren().add(t);

    

    root.getChildren().add(g);
    primaryStage.setScene(scene);
    primaryStage.show();
  }
}

The code above generates the following result.

null

Example 2

Calling the setFraction() method with 0.7f is essentially specifying that we want 70 percent of the reflection to be shown.

The following code shows how to use reflection effect on text.

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.effect.Reflection;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;
import javafx.stage.Stage;
/*from   w  w w. ja  v a  2 s  . c  om*/
public class Main extends Application {
    public static void main(String[] args) {
        Application.launch(args);
    }
    
    @Override
    public void start(Stage primaryStage) {
        primaryStage.setTitle("");
        Group root = new Group();
        Scene scene = new Scene(root, 300, 250, Color.WHITE);
        
        Text t = new Text();
        t.setX(10.0);
        t.setY(50.0);
        t.setCache(true);
        t.setText("Reflections on JavaFX...");
        t.setFill(Color.RED);
        t.setFont(Font.font(null, FontWeight.BOLD, 30));

        Reflection r = new Reflection();
        r.setFraction(0.7);

        t.setEffect(r);
        
        root.getChildren().add(t); 
        primaryStage.setScene(scene);
        primaryStage.show();
    }
}

The reflection values range from zero (0%) to one (100%).

We can also set the space between the opaque node portion and the reflection portion by the setTopOffset() method. The top offset defaults to zero.

The code above generates the following result.

null

Example 3

The following code shows how to do line breaking for Text with line separators.

import javafx.application.Application;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.effect.InnerShadow;
import javafx.scene.effect.InnerShadowBuilder;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;
import javafx.scene.text.TextBuilder;
import javafx.stage.Stage;
//  www  .ja v a 2s. c om
public class Main extends Application {
    public static void main(String[] args) {
        Application.launch(args);
    }
    @Override
    public void start(Stage primaryStage) {
        primaryStage.setTitle("Keyboard");
        Group root = new Group();
        Scene scene = new Scene(root, 530, 300, Color.WHITE);

        final StringProperty statusProperty = new SimpleStringProperty();

        InnerShadow iShadow = InnerShadowBuilder.create()
                .offsetX(3.5f)
                .offsetY(3.5f)
                .build();
        final Text status = TextBuilder.create()
            .effect(iShadow)
            .x(100)
            .y(50)
            .fill(Color.LIME)
            .font(Font.font(null, FontWeight.BOLD, 35))
            .translateY(50)
            .build();
        status.textProperty().bind(statusProperty);
        statusProperty.set("Line\nLine2\nLine");
        root.getChildren().add(status);
        
        primaryStage.setScene(scene);
        primaryStage.show();
    }
}

The code above generates the following result.

null

Example 4

The following code shows how to set Text wrapping width.

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import javafx.stage.Stage;
//from   ww  w .  jav a 2 s.c om
public class Main extends Application {

  @Override
  public void start(Stage stage) {
    Group root = new Group();
    Scene scene = new Scene(root, 300, 150);
    stage.setScene(scene);
    stage.setTitle("Sample");

    Text t = new Text(10, 50, "This is a test");
    t.setWrappingWidth(200);
    t.setText("First row Second row Second row Second row Second row Second row ");
    t.setFont(new Font(20));

    root.getChildren().add(t);


    stage.show();
  }

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

The code above generates the following result.

null