Class Text

All Implemented Interfaces:
Styleable, EventTarget

@DefaultProperty("text") public non-sealed class Text extends Shape
The Text class defines a node that displays a text. Paragraphs are separated by '\n' and the text is wrapped on paragraph boundaries.
import javafx.scene.text.*;

Text t = new Text(10, 50, "This is a test");
t.setFont(new Font(20));
import javafx.scene.text.*;

Text t = new Text();
text.setFont(new Font(20));
text.setText("First row\nSecond row");
import javafx.scene.text.*;

Text t = new Text();
text.setFont(new Font(20));
text.setWrappingWidth(200);
text.setTextAlignment(TextAlignment.JUSTIFY)
text.setText("The quick brown fox jumps over the lazy dog");
Since:
JavaFX 2.0
  • Property Details

  • Constructor Details

    • Text

      public Text()
      Creates an empty instance of Text.
    • Text

      public Text(String text)
      Creates an instance of Text containing the given string.
      Parameters:
      text - text to be contained in the instance
    • Text

      public Text(double x, double y, String text)
      Creates an instance of Text on the given coordinates containing the given string.
      Parameters:
      x - the horizontal position of the text
      y - the vertical position of the text
      text - text to be contained in the instance
  • Method Details

    • setText

      public final void setText(String value)
      Sets the value of the text property.
      Property description:
      Defines text string that is to be displayed.
      Default value:
      empty string
      Parameters:
      value - the value for the text property
      See Also:
    • getText

      public final String getText()
      Gets the value of the text property.
      Property description:
      Defines text string that is to be displayed.
      Default value:
      empty string
      Returns:
      the value of the text property
      See Also:
    • textProperty

      public final StringProperty textProperty()
      Defines text string that is to be displayed.
      Default value:
      empty string
      Returns:
      the text property
      See Also:
    • setX

      public final void setX(double value)
      Sets the value of the x property.
      Property description:
      Defines the X coordinate of text origin.
      Default value:
      0
      Parameters:
      value - the value for the x property
      See Also:
    • getX

      public final double getX()
      Gets the value of the x property.
      Property description:
      Defines the X coordinate of text origin.
      Default value:
      0
      Returns:
      the value of the x property
      See Also:
    • xProperty

      public final DoubleProperty xProperty()
      Defines the X coordinate of text origin.
      Default value:
      0
      Returns:
      the x property
      See Also:
    • setY

      public final void setY(double value)
      Sets the value of the y property.
      Property description:
      Defines the Y coordinate of text origin.
      Default value:
      0
      Parameters:
      value - the value for the y property
      See Also:
    • getY

      public final double getY()
      Gets the value of the y property.
      Property description:
      Defines the Y coordinate of text origin.
      Default value:
      0
      Returns:
      the value of the y property
      See Also:
    • yProperty

      public final DoubleProperty yProperty()
      Defines the Y coordinate of text origin.
      Default value:
      0
      Returns:
      the y property
      See Also:
    • setFont

      public final void setFont(Font value)
      Sets the value of the font property.
      Property description:
      Defines the font of text.
      Default value:
      Font{}
      Parameters:
      value - the value for the font property
      See Also:
    • getFont

      public final Font getFont()
      Gets the value of the font property.
      Property description:
      Defines the font of text.
      Default value:
      Font{}
      Returns:
      the value of the font property
      See Also:
    • fontProperty

      public final ObjectProperty<Font> fontProperty()
      Defines the font of text.
      Default value:
      Font{}
      Returns:
      the font property
      See Also:
    • setTextOrigin

      public final void setTextOrigin(VPos value)
      Sets the value of the textOrigin property.
      Property description:
      Defines the origin of text coordinate system in local coordinates. Note: in case multiple rows are rendered VPos.BASELINE and VPos.TOP define the origin of the top row while VPos.BOTTOM defines the origin of the bottom row.
      Default value:
      VPos.BASELINE
      Parameters:
      value - the value for the textOrigin property
      See Also:
    • getTextOrigin

      public final VPos getTextOrigin()
      Gets the value of the textOrigin property.
      Property description:
      Defines the origin of text coordinate system in local coordinates. Note: in case multiple rows are rendered VPos.BASELINE and VPos.TOP define the origin of the top row while VPos.BOTTOM defines the origin of the bottom row.
      Default value:
      VPos.BASELINE
      Returns:
      the value of the textOrigin property
      See Also:
    • textOriginProperty

      public final ObjectProperty<VPos> textOriginProperty()
      Defines the origin of text coordinate system in local coordinates. Note: in case multiple rows are rendered VPos.BASELINE and VPos.TOP define the origin of the top row while VPos.BOTTOM defines the origin of the bottom row.
      Default value:
      VPos.BASELINE
      Returns:
      the origin of text coordinate system in local coordinates
      See Also:
    • setBoundsType

      public final void setBoundsType(TextBoundsType value)
      Sets the value of the boundsType property.
      Property description:
      Determines how the bounds of the text node are calculated. Logical bounds is a more appropriate default for text than the visual bounds. See TextBoundsType for more information.
      Default value:
      TextBoundsType.LOGICAL
      Parameters:
      value - the value for the boundsType property
      See Also:
    • getBoundsType

      public final TextBoundsType getBoundsType()
      Gets the value of the boundsType property.
      Property description:
      Determines how the bounds of the text node are calculated. Logical bounds is a more appropriate default for text than the visual bounds. See TextBoundsType for more information.
      Default value:
      TextBoundsType.LOGICAL
      Returns:
      the value of the boundsType property
      See Also:
    • boundsTypeProperty

      public final ObjectProperty<TextBoundsType> boundsTypeProperty()
      Determines how the bounds of the text node are calculated. Logical bounds is a more appropriate default for text than the visual bounds. See TextBoundsType for more information.
      Default value:
      TextBoundsType.LOGICAL
      Returns:
      the boundsType property
      See Also:
    • setWrappingWidth

      public final void setWrappingWidth(double value)
      Sets the value of the wrappingWidth property.
      Property description:
      Defines a width constraint for the text in user space coordinates. The width is measured in pixels (and not glyph or character count). If the value is > 0 text will be line wrapped as needed to satisfy this constraint.
      Default value:
      0
      Parameters:
      value - the value for the wrappingWidth property
      See Also:
    • getWrappingWidth

      public final double getWrappingWidth()
      Gets the value of the wrappingWidth property.
      Property description:
      Defines a width constraint for the text in user space coordinates. The width is measured in pixels (and not glyph or character count). If the value is > 0 text will be line wrapped as needed to satisfy this constraint.
      Default value:
      0
      Returns:
      the value of the wrappingWidth property
      See Also:
    • wrappingWidthProperty

      public final DoubleProperty wrappingWidthProperty()
      Defines a width constraint for the text in user space coordinates. The width is measured in pixels (and not glyph or character count). If the value is > 0 text will be line wrapped as needed to satisfy this constraint.
      Default value:
      0
      Returns:
      the wrappingWidth property
      See Also:
    • setUnderline

      public final void setUnderline(boolean value)
      Sets the value of the underline property.
      Property description:
      Defines if each line of text should have a line below it.
      Default value:
      false
      Parameters:
      value - the value for the underline property
      See Also:
    • isUnderline

      public final boolean isUnderline()
      Gets the value of the underline property.
      Property description:
      Defines if each line of text should have a line below it.
      Default value:
      false
      Returns:
      the value of the underline property
      See Also:
    • underlineProperty

      public final BooleanProperty underlineProperty()
      Defines if each line of text should have a line below it.
      Default value:
      false
      Returns:
      if each line of text should have a line below it
      See Also:
    • setStrikethrough

      public final void setStrikethrough(boolean value)
      Sets the value of the strikethrough property.
      Property description:
      Defines if each line of text should have a line through it.
      Default value:
      false
      Parameters:
      value - the value for the strikethrough property
      See Also:
    • isStrikethrough

      public final boolean isStrikethrough()
      Gets the value of the strikethrough property.
      Property description:
      Defines if each line of text should have a line through it.
      Default value:
      false
      Returns:
      the value of the strikethrough property
      See Also:
    • strikethroughProperty

      public final BooleanProperty strikethroughProperty()
      Defines if each line of text should have a line through it.
      Default value:
      false
      Returns:
      if each line of text should have a line through it
      See Also:
    • setTextAlignment

      public final void setTextAlignment(TextAlignment value)
      Sets the value of the textAlignment property.
      Property description:
      Defines horizontal text alignment in the bounding box. The width of the bounding box is defined by the widest row. Note: In the case of a single line of text, where the width of the node is determined by the width of the text, the alignment setting has no effect.
      Default value:
      TextAlignment.LEFT
      Parameters:
      value - the value for the textAlignment property
      See Also:
    • getTextAlignment

      public final TextAlignment getTextAlignment()
      Gets the value of the textAlignment property.
      Property description:
      Defines horizontal text alignment in the bounding box. The width of the bounding box is defined by the widest row. Note: In the case of a single line of text, where the width of the node is determined by the width of the text, the alignment setting has no effect.
      Default value:
      TextAlignment.LEFT
      Returns:
      the value of the textAlignment property
      See Also:
    • textAlignmentProperty

      public final ObjectProperty<TextAlignment> textAlignmentProperty()
      Defines horizontal text alignment in the bounding box. The width of the bounding box is defined by the widest row. Note: In the case of a single line of text, where the width of the node is determined by the width of the text, the alignment setting has no effect.
      Default value:
      TextAlignment.LEFT
      Returns:
      the horizontal text alignment in the bounding box
      See Also:
    • setLineSpacing

      public final void setLineSpacing(double spacing)
      Sets the value of the lineSpacing property.
      Property description:
      Defines the vertical space in pixel between lines.
      Default value:
      0
      Parameters:
      spacing - the value for the lineSpacing property
      Since:
      JavaFX 8.0
      See Also:
    • getLineSpacing

      public final double getLineSpacing()
      Gets the value of the lineSpacing property.
      Property description:
      Defines the vertical space in pixel between lines.
      Default value:
      0
      Returns:
      the value of the lineSpacing property
      Since:
      JavaFX 8.0
      See Also:
    • lineSpacingProperty

      public final DoubleProperty lineSpacingProperty()
      Defines the vertical space in pixel between lines.
      Default value:
      0
      Returns:
      the vertical space in pixel between lines
      Since:
      JavaFX 8.0
      See Also:
    • getBaselineOffset

      public final double getBaselineOffset()
      Gets the value of the baselineOffset property.
      Overrides:
      getBaselineOffset in class Node
      Property description:
      The 'alphabetic' (or roman) baseline offset from the Text node's layoutBounds.minY location. The value typically corresponds to the max ascent of the font.
      Returns:
      the value of the baselineOffset property
      See Also:
    • baselineOffsetProperty

      public final ReadOnlyDoubleProperty baselineOffsetProperty()
      The 'alphabetic' (or roman) baseline offset from the Text node's layoutBounds.minY location. The value typically corresponds to the max ascent of the font.
      Returns:
      the baseline offset from this text node
      See Also:
    • setFontSmoothingType

      public final void setFontSmoothingType(FontSmoothingType value)
      Sets the value of the fontSmoothingType property.
      Property description:
      Specifies a requested font smoothing type: gray or LCD.

      Note: LCD mode doesn't apply in numerous cases, such as various compositing modes, where effects are applied and very large glyphs.

      Default value:
      FontSmoothingType.GRAY
      Parameters:
      value - the value for the fontSmoothingType property
      Since:
      JavaFX 2.1
      See Also:
    • getFontSmoothingType

      public final FontSmoothingType getFontSmoothingType()
      Gets the value of the fontSmoothingType property.
      Property description:
      Specifies a requested font smoothing type: gray or LCD.

      Note: LCD mode doesn't apply in numerous cases, such as various compositing modes, where effects are applied and very large glyphs.

      Default value:
      FontSmoothingType.GRAY
      Returns:
      the value of the fontSmoothingType property
      Since:
      JavaFX 2.1
      See Also:
    • fontSmoothingTypeProperty

      public final ObjectProperty<FontSmoothingType> fontSmoothingTypeProperty()
      Specifies a requested font smoothing type: gray or LCD.

      Note: LCD mode doesn't apply in numerous cases, such as various compositing modes, where effects are applied and very large glyphs.

      Default value:
      FontSmoothingType.GRAY
      Returns:
      the fontSmoothingType property
      Since:
      JavaFX 2.1
      See Also:
    • getSelectionShape

      public final PathElement[] getSelectionShape()
      Gets the value of the selectionShape property.
      Property description:
      The shape of the selection in local coordinates.
      Returns:
      the value of the selectionShape property
      Since:
      9
      See Also:
    • selectionShapeProperty

      public final ReadOnlyObjectProperty<PathElement[]> selectionShapeProperty()
      The shape of the selection in local coordinates.
      Returns:
      the selectionShape property
      Since:
      9
      See Also:
    • setSelectionStart

      public final void setSelectionStart(int value)
      Sets the value of the selectionStart property.
      Property description:
      The start index of the selection in the content. If the value is -1, the selection is unset.
      Default value:
      -1
      Parameters:
      value - the value for the selectionStart property
      Since:
      9
      See Also:
    • getSelectionStart

      public final int getSelectionStart()
      Gets the value of the selectionStart property.
      Property description:
      The start index of the selection in the content. If the value is -1, the selection is unset.
      Default value:
      -1
      Returns:
      the value of the selectionStart property
      Since:
      9
      See Also:
    • selectionStartProperty

      public final IntegerProperty selectionStartProperty()
      The start index of the selection in the content. If the value is -1, the selection is unset.
      Default value:
      -1
      Returns:
      the selectionStart property
      Since:
      9
      See Also:
    • setSelectionEnd

      public final void setSelectionEnd(int value)
      Sets the value of the selectionEnd property.
      Property description:
      The end index of the selection in the content. If the value is -1, the selection is unset.
      Default value:
      -1
      Parameters:
      value - the value for the selectionEnd property
      Since:
      9
      See Also:
    • getSelectionEnd

      public final int getSelectionEnd()
      Gets the value of the selectionEnd property.
      Property description:
      The end index of the selection in the content. If the value is -1, the selection is unset.
      Default value:
      -1
      Returns:
      the value of the selectionEnd property
      Since:
      9
      See Also:
    • selectionEndProperty

      public final IntegerProperty selectionEndProperty()
      The end index of the selection in the content. If the value is -1, the selection is unset.
      Default value:
      -1
      Returns:
      the selectionEnd property
      Since:
      9
      See Also:
    • selectionFillProperty

      public final ObjectProperty<Paint> selectionFillProperty()
      The fill color of selected text.
      Returns:
      the fill color of selected text
      Since:
      9
      See Also:
    • setSelectionFill

      public final void setSelectionFill(Paint paint)
      Sets the value of the selectionFill property.
      Property description:
      The fill color of selected text.
      Parameters:
      paint - the value for the selectionFill property
      Since:
      9
      See Also:
    • getSelectionFill

      public final Paint getSelectionFill()
      Gets the value of the selectionFill property.
      Property description:
      The fill color of selected text.
      Returns:
      the value of the selectionFill property
      Since:
      9
      See Also:
    • getCaretShape

      public final PathElement[] getCaretShape()
      Gets the value of the caretShape property.
      Property description:
      The shape of caret, in local coordinates.
      Returns:
      the value of the caretShape property
      Since:
      9
      See Also:
    • caretShapeProperty

      public final ReadOnlyObjectProperty<PathElement[]> caretShapeProperty()
      The shape of caret, in local coordinates.
      Returns:
      the caretShape property
      Since:
      9
      See Also:
    • setCaretPosition

      public final void setCaretPosition(int value)
      Sets the value of the caretPosition property.
      Property description:
      The caret index in the content. If the value is -1, the caret is unset.
      Default value:
      -1
      Parameters:
      value - the value for the caretPosition property
      Since:
      9
      See Also:
    • getCaretPosition

      public final int getCaretPosition()
      Gets the value of the caretPosition property.
      Property description:
      The caret index in the content. If the value is -1, the caret is unset.
      Default value:
      -1
      Returns:
      the value of the caretPosition property
      Since:
      9
      See Also:
    • caretPositionProperty

      public final IntegerProperty caretPositionProperty()
      The caret index in the content. If the value is -1, the caret is unset.
      Default value:
      -1
      Returns:
      the caretPosition property
      Since:
      9
      See Also:
    • setCaretBias

      public final void setCaretBias(boolean value)
      Sets the value of the caretBias property.
      Property description:
      The type of caret bias in the content. If true, the bias is towards the leading character edge, otherwise, the bias is towards the trailing character edge.
      Default value:
      true
      Parameters:
      value - the value for the caretBias property
      Since:
      9
      See Also:
    • isCaretBias

      public final boolean isCaretBias()
      Gets the value of the caretBias property.
      Property description:
      The type of caret bias in the content. If true, the bias is towards the leading character edge, otherwise, the bias is towards the trailing character edge.
      Default value:
      true
      Returns:
      the value of the caretBias property
      Since:
      9
      See Also:
    • caretBiasProperty

      public final BooleanProperty caretBiasProperty()
      The type of caret bias in the content. If true, the bias is towards the leading character edge, otherwise, the bias is towards the trailing character edge.
      Default value:
      true
      Returns:
      the caretBias property
      Since:
      9
      See Also:
    • hitTest

      public final HitInfo hitTest(Point2D point)
      Maps local point to HitInfo in the content.
      Parameters:
      point - the specified point to be tested
      Returns:
      a HitInfo representing the character index found
      Since:
      9
    • caretShape

      public final PathElement[] caretShape(int charIndex, boolean caretBias)
      Returns the shape for the caret at the given index and bias.
      Parameters:
      charIndex - the character index for the caret
      caretBias - whether the caret is biased on the leading edge of the character
      Returns:
      an array of PathElement which can be used to create a Shape
      Since:
      9
    • rangeShape

      public final PathElement[] rangeShape(int start, int end)
      Returns the shape for the range of the text in local coordinates.
      Parameters:
      start - the beginning character index for the range
      end - the end character index (non-inclusive) for the range
      Returns:
      an array of PathElement which can be used to create a Shape
      Since:
      9
    • underlineShape

      public final PathElement[] underlineShape(int start, int end)
      Returns the shape for the underline in local coordinates.
      Parameters:
      start - the beginning character index for the range
      end - the end character index (non-inclusive) for the range
      Returns:
      an array of PathElement which can be used to create a Shape
      Since:
      9
    • tabSizeProperty

      public final IntegerProperty tabSizeProperty()
      The size of a tab stop in spaces. Values less than 1 are treated as 1.
      Default value:
      8
      Returns:
      the tabSize property
      Since:
      14
      See Also:
    • getTabSize

      public final int getTabSize()
      Gets the value of the tabSize property.
      Property description:
      The size of a tab stop in spaces. Values less than 1 are treated as 1.
      Default value:
      8
      Returns:
      the value of the tabSize property
      Since:
      14
      See Also:
    • setTabSize

      public final void setTabSize(int spaces)
      Sets the value of the tabSize property.
      Property description:
      The size of a tab stop in spaces. Values less than 1 are treated as 1.
      Default value:
      8
      Parameters:
      spaces - the value for the tabSize property
      Since:
      14
      See Also:
    • getClassCssMetaData

      public static List<CssMetaData<? extends Styleable,?>> getClassCssMetaData()
      Gets the CssMetaData associated with this class, which may include the CssMetaData of its superclasses.
      Returns:
      the CssMetaData
      Since:
      JavaFX 8.0
    • getCssMetaData

      public List<CssMetaData<? extends Styleable,?>> getCssMetaData()
      This method should delegate to Node.getClassCssMetaData() so that a Node's CssMetaData can be accessed without the need for reflection.
      Specified by:
      getCssMetaData in interface Styleable
      Overrides:
      getCssMetaData in class Shape
      Returns:
      The CssMetaData associated with this node, which may include the CssMetaData of its superclasses.
      Since:
      JavaFX 8.0
    • toString

      public String toString()
      Returns a string representation of this Text object.
      Overrides:
      toString in class Node
      Returns:
      a string representation of this Text object.