How to strike through and underline text in JavaFX?


In JavaFX, the text node is represented by the Javafx.scene.text.Text class. To insert/display text in JavaFx window you need to −

  • Instantiate the Text class.

  • Set the basic properties like position and text string, using the setter methods or, bypassing them as arguments to the constructor.

  • Add the created node to the Group object.

The strikethrough property of the javafx.scene.text.Text class determines whether each line of the text should have a straight line passing through the middle of it. You can set the value to this property using the setStrikeThrough() method. It accepts a boolean value. You can strike though the text (node) by passing true as an argument to this method.

The underline property of the javafx.scene.text.Text class determines whether each line of the text should have a straight line below it. You can set the value to this property using the setUnderline() method. It accepts a boolean value. You can have a line below the text (node) by passing true as an argument to this method.

Example

import java.io.FileNotFoundException;
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.Font;
import javafx.scene.text.FontPosture;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;
public class Underline_StrikeThrough extends Application {
   public void start(Stage stage) throws FileNotFoundException {
      //Creating a text object
      String str = "Welcome to Tutorialspoint";
      Text text = new Text(30.0, 80.0, str);
      //Setting the font
      Font font = Font.font("Brush Script MT", FontWeight.BOLD, FontPosture.REGULAR, 65);
      text.setFont(font);
      //Setting the color of the text
      text.setFill(Color.DARKCYAN);
      //Setting the width and color of the stroke
      text.setStrokeWidth(2);
      text.setStroke(Color.DARKSLATEGRAY);
      //Underlining the text
      text.setUnderline(true);
      //Striking through the text
      text.setStrikethrough(true);
      //Setting the stage
      Group root = new Group(text);
      Scene scene = new Scene(root, 595, 150, Color.BEIGE);
      stage.setTitle("Underline And Strike-through");
      stage.setScene(scene);
      stage.show();
   }
   public static void main(String args[]){
      launch(args);
   }
}

Output

Updated on: 14-Apr-2020

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements