Class Text

All Implemented Interfaces:
Styleable, EventTarget

@DefaultProperty("text")
public 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 property text.
      Property description:
      Defines text string that is to be displayed.
      Default value:
      empty string
    • getText

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

      public final StringProperty textProperty()
      Defines text string that is to be displayed.
      Default value:
      empty string
      See Also:
      getText(), setText(String)
    • setX

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

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

      public final DoubleProperty xProperty()
      Defines the X coordinate of text origin.
      Default value:
      0
      See Also:
      getX(), setX(double)
    • setY

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

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

      public final DoubleProperty yProperty()
      Defines the Y coordinate of text origin.
      Default value:
      0
      See Also:
      getY(), setY(double)
    • setFont

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

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

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

      public final void setTextOrigin​(VPos value)
      Sets the value of the property textOrigin.
      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
    • getTextOrigin

      public final VPos getTextOrigin()
      Gets the value of the property textOrigin.
      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
    • 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
      See Also:
      getTextOrigin(), setTextOrigin(VPos)
    • setBoundsType

      public final void setBoundsType​(TextBoundsType value)
      Sets the value of the property boundsType.
      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
    • getBoundsType

      public final TextBoundsType getBoundsType()
      Gets the value of the property boundsType.
      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
    • 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
      See Also:
      getBoundsType(), setBoundsType(TextBoundsType)
    • setWrappingWidth

      public final void setWrappingWidth​(double value)
      Sets the value of the property wrappingWidth.
      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
    • getWrappingWidth

      public final double getWrappingWidth()
      Gets the value of the property wrappingWidth.
      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
    • 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
      See Also:
      getWrappingWidth(), setWrappingWidth(double)
    • setUnderline

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

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

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

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

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

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

      public final void setTextAlignment​(TextAlignment value)
      Sets the value of the property textAlignment.
      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
    • getTextAlignment

      public final TextAlignment getTextAlignment()
      Gets the value of the property textAlignment.
      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
    • 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
      See Also:
      getTextAlignment(), setTextAlignment(TextAlignment)
    • setLineSpacing

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

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

      public final DoubleProperty lineSpacingProperty()
      Defines the vertical space in pixel between lines.
      Default value:
      0
      Since:
      JavaFX 8.0
      See Also:
      getLineSpacing(), setLineSpacing(double)
    • 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.
      See Also:
      Node.getBaselineOffset()
    • setFontSmoothingType

      public final void setFontSmoothingType​(FontSmoothingType value)
      Sets the value of the property fontSmoothingType.
      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
      Since:
      JavaFX 2.1
    • getFontSmoothingType

      public final FontSmoothingType getFontSmoothingType()
      Gets the value of the property fontSmoothingType.
      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
      Since:
      JavaFX 2.1
    • 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
      Since:
      JavaFX 2.1
      See Also:
      getFontSmoothingType(), setFontSmoothingType(FontSmoothingType)
    • getSelectionShape

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

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

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

      public final int getSelectionStart()
      Gets the value of the property selectionStart.
      Property description:
      The start index of the selection in the content. If the value is -1, the selection is unset.
      Default value:
      -1
      Since:
      9
    • 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
      Since:
      9
      See Also:
      getSelectionStart(), setSelectionStart(int)
    • setSelectionEnd

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

      public final int getSelectionEnd()
      Gets the value of the property selectionEnd.
      Property description:
      The end index of the selection in the content. If the value is -1, the selection is unset.
      Default value:
      -1
      Since:
      9
    • 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
      Since:
      9
      See Also:
      getSelectionEnd(), setSelectionEnd(int)
    • selectionFillProperty

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

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

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

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

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

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

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

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

      public final void setCaretBias​(boolean value)
      Sets the value of the property caretBias.
      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
      Since:
      9
    • isCaretBias

      public final boolean isCaretBias()
      Gets the value of the property caretBias.
      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
      Since:
      9
    • 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
      Since:
      9
      See Also:
      isCaretBias(), setCaretBias(boolean)
    • hitTest

      public final HitInfo hitTest​(Point2D point)
      Maps local point to index 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
      Since:
      14
      See Also:
      getTabSize(), setTabSize(int)
    • getTabSize

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

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

      public static List<CssMetaData<? extends Styleable,​?>> getClassCssMetaData()
      Returns:
      The CssMetaData associated with this class, which may include the CssMetaData of its superclasses.
      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.