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
    • Constructor Detail

      • 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 Detail

      • usesMirroring

        public boolean usesMirroring()
        Description copied from class: Node
        Determines whether a node should be mirrored when node orientation is right-to-left.

        When a node is mirrored, the origin is automatically moved to the top right corner causing the node to layout children and draw from right to left using a mirroring transformation. Some nodes may wish to draw from right to left without using a transformation. These nodes will will answer false and implement right-to-left orientation without using the automatic transformation.

        Overrides:
        usesMirroring in class Node
        Returns:
        true if this Node should be mirrored
      • 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
      • 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
      • 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
      • 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{}
      • 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, e.g. pixels, 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, e.g. pixels, 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, e.g. pixels, 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
      • 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
      • 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
      • getBaselineOffset

        public final double getBaselineOffset()
        Gets the value of the property baselineOffset.
        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:
        offset of text baseline from layoutBounds.minY for non-resizable Nodes or Node.BASELINE_OFFSET_SAME_AS_HEIGHT otherwise
      • 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:
        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. The width of the bounding box is defined by the widest row. 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. The width of the bounding box is defined by the widest row. 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. The width of the bounding box is defined by the widest row. 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()
        Shape of selection in local coordinates.
        Returns:
        the shape of selection in local coordinates
        Since:
        9
      • setSelectionStart

        public final void setSelectionStart​(int value)
        Selection start index in the content. Set to -1 to unset selection.
        Parameters:
        value - the selection start index
        Since:
        9
      • getSelectionStart

        public final int getSelectionStart()
        Gets the value of the property selectionStart.
        Property description:
      • setSelectionEnd

        public final void setSelectionEnd​(int value)
        Selection end index in the content. Set to -1 to unset selection.
        Parameters:
        value - the selection end index
        Since:
        9
      • getSelectionEnd

        public final int getSelectionEnd()
        Gets the value of the property selectionEnd.
        Property description:
      • 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()
        Shape of caret in local coordinates.
        Returns:
        the shape of caret in local coordinates
        Since:
        9
      • setCaretPosition

        public final void setCaretPosition​(int value)
        Caret index in the content. Set to -1 to unset caret.
        Parameters:
        value - the caret index in the content
        Since:
        9
      • getCaretPosition

        public final int getCaretPosition()
        Gets the value of the property caretPosition.
        Property description:
      • setCaretBias

        public final void setCaretBias​(boolean value)
        caret bias in the content. true means a bias towards the leading character edge. (true=leading/false=trailing)
        Parameters:
        value - the caret bias in the content
        Since:
        9
      • isCaretBias

        public final boolean isCaretBias()
        Gets the value of the property caretBias.
        Property description:
      • 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 shape for the caret at 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 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 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
      • 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
      • 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.
      • queryAccessibleAttribute

        public Object queryAccessibleAttribute​(AccessibleAttribute attribute,
                                               Object... parameters)
        This method is called by the assistive technology to request the value for an attribute.

        This method is commonly overridden by subclasses to implement attributes that are required for a specific role.
        If a particular attribute is not handled, the superclass implementation must be called.

        Overrides:
        queryAccessibleAttribute in class Node
        Parameters:
        attribute - the requested attribute
        parameters - optional list of parameters
        Returns:
        the value for the requested attribute
        See Also:
        AccessibleAttribute