Class TextElement

java.lang.Object
com.vaadin.flow.dom.Node<com.vaadin.flow.dom.Element>
com.vaadin.flow.dom.Element
All Implemented Interfaces:
Serializable

public class TextElement extends SvgGraphicsElement
A typed Java API for the SVG <text> element.

The <text> element defines a graphics element consisting of text. It's possible to apply a gradient, pattern, clipping path, mask, or filter to text like any other SVG graphics element.

Write-Only vs Read-Write Methods

This class provides two variants for each attribute setter:

  • Default methods (e.g., x(), y()) - Use an optimized write-only approach. Attribute values are NOT stored on the server and cannot be retrieved via getAttribute().
  • RW methods (e.g., xRW(), yRW()) - Use traditional setAttribute() which stores values on the server for later retrieval.
See Also:
  • Constructor Details

    • TextElement

      public TextElement()
    • TextElement

      public TextElement(String text)
      Creates a text element with the given text content.
      Parameters:
      text - the text content
    • TextElement

      public TextElement(double x, double y, String text)
      Creates a text element at the specified position.
      Parameters:
      x - the x coordinate
      y - the y coordinate
      text - the text content
  • Method Details

    • text

      public TextElement text(String text)
      Sets the text content.
      Parameters:
      text - the text content
      Returns:
      this element for method chaining
    • x

      public TextElement x(double x)
      Sets the x coordinate of the text starting point.

      Uses write-only optimization. Use xRW(double) if you need to read the value back.

      Parameters:
      x - the x coordinate
      Returns:
      this element for method chaining
    • x

      public TextElement x(String x)
      Sets the x coordinate with a unit.

      Uses write-only optimization. Use xRW(String) if you need to read the value back.

      Parameters:
      x - the x coordinate (e.g., "50%")
      Returns:
      this element for method chaining
    • xRW

      public TextElement xRW(double x)
      Sets the x coordinate of the text starting point (read-write).
      Parameters:
      x - the x coordinate
      Returns:
      this element for method chaining
    • xRW

      public TextElement xRW(String x)
      Sets the x coordinate with a unit (read-write).
      Parameters:
      x - the x coordinate (e.g., "50%")
      Returns:
      this element for method chaining
    • y

      public TextElement y(double y)
      Sets the y coordinate of the text starting point.

      Uses write-only optimization. Use yRW(double) if you need to read the value back.

      Parameters:
      y - the y coordinate
      Returns:
      this element for method chaining
    • y

      public TextElement y(String y)
      Sets the y coordinate with a unit.

      Uses write-only optimization. Use yRW(String) if you need to read the value back.

      Parameters:
      y - the y coordinate (e.g., "50%")
      Returns:
      this element for method chaining
    • yRW

      public TextElement yRW(double y)
      Sets the y coordinate of the text starting point (read-write).
      Parameters:
      y - the y coordinate
      Returns:
      this element for method chaining
    • yRW

      public TextElement yRW(String y)
      Sets the y coordinate with a unit (read-write).
      Parameters:
      y - the y coordinate (e.g., "50%")
      Returns:
      this element for method chaining
    • position

      public TextElement position(double x, double y)
      Sets the position of the text.

      Uses write-only optimization. Use positionRW(double, double) if you need to read the values back.

      Parameters:
      x - the x coordinate
      y - the y coordinate
      Returns:
      this element for method chaining
    • positionRW

      public TextElement positionRW(double x, double y)
      Sets the position of the text (read-write).
      Parameters:
      x - the x coordinate
      y - the y coordinate
      Returns:
      this element for method chaining
    • dx

      public TextElement dx(double dx)
      Sets the horizontal shift from the previous text position.

      Uses write-only optimization. Use dxRW(double) if you need to read the value back.

      Parameters:
      dx - the horizontal offset
      Returns:
      this element for method chaining
    • dxRW

      public TextElement dxRW(double dx)
      Sets the horizontal shift from the previous text position (read-write).
      Parameters:
      dx - the horizontal offset
      Returns:
      this element for method chaining
    • dy

      public TextElement dy(double dy)
      Sets the vertical shift from the previous text position.

      Uses write-only optimization. Use dyRW(double) if you need to read the value back.

      Parameters:
      dy - the vertical offset
      Returns:
      this element for method chaining
    • dyRW

      public TextElement dyRW(double dy)
      Sets the vertical shift from the previous text position (read-write).
      Parameters:
      dy - the vertical offset
      Returns:
      this element for method chaining
    • rotate

      public TextElement rotate(String rotate)
      Sets the rotation for each character.

      Uses write-only optimization. Use rotateRW(String) if you need to read the value back.

      Parameters:
      rotate - the rotation angle(s) in degrees
      Returns:
      this element for method chaining
    • rotateRW

      public TextElement rotateRW(String rotate)
      Sets the rotation for each character (read-write).
      Parameters:
      rotate - the rotation angle(s) in degrees
      Returns:
      this element for method chaining
    • textLength

      public TextElement textLength(double length)
      Sets the total length of the text.

      Uses write-only optimization. Use textLengthRW(double) if you need to read the value back.

      Parameters:
      length - the text length
      Returns:
      this element for method chaining
    • textLengthRW

      public TextElement textLengthRW(double length)
      Sets the total length of the text (read-write).
      Parameters:
      length - the text length
      Returns:
      this element for method chaining
    • textAnchor

      public TextElement textAnchor(TextElement.TextAnchor anchor)
      Sets the text anchor (horizontal alignment).

      Uses write-only optimization. Use textAnchorRW(TextAnchor) if you need to read the value back.

      Parameters:
      anchor - the text anchor
      Returns:
      this element for method chaining
    • textAnchorRW

      public TextElement textAnchorRW(TextElement.TextAnchor anchor)
      Sets the text anchor (horizontal alignment) (read-write).
      Parameters:
      anchor - the text anchor
      Returns:
      this element for method chaining
    • dominantBaseline

      public TextElement dominantBaseline(TextElement.DominantBaseline baseline)
      Sets the dominant baseline (vertical alignment).

      Uses write-only optimization. Use dominantBaselineRW(DominantBaseline) if you need to read the value back.

      Parameters:
      baseline - the dominant baseline
      Returns:
      this element for method chaining
    • dominantBaselineRW

      public TextElement dominantBaselineRW(TextElement.DominantBaseline baseline)
      Sets the dominant baseline (vertical alignment) (read-write).
      Parameters:
      baseline - the dominant baseline
      Returns:
      this element for method chaining
    • fontFamily

      public TextElement fontFamily(String fontFamily)
      Sets the font family.

      Uses write-only optimization. Use fontFamilyRW(String) if you need to read the value back.

      Parameters:
      fontFamily - the font family (e.g., "Arial", "sans-serif")
      Returns:
      this element for method chaining
    • fontFamilyRW

      public TextElement fontFamilyRW(String fontFamily)
      Sets the font family (read-write).
      Parameters:
      fontFamily - the font family (e.g., "Arial", "sans-serif")
      Returns:
      this element for method chaining
    • fontSize

      public TextElement fontSize(double size)
      Sets the font size.

      Uses write-only optimization. Use fontSizeRW(double) if you need to read the value back.

      Parameters:
      size - the font size
      Returns:
      this element for method chaining
    • fontSize

      public TextElement fontSize(String size)
      Sets the font size with a unit.

      Uses write-only optimization. Use fontSizeRW(String) if you need to read the value back.

      Parameters:
      size - the font size (e.g., "12px", "1em")
      Returns:
      this element for method chaining
    • fontSizeRW

      public TextElement fontSizeRW(double size)
      Sets the font size (read-write).
      Parameters:
      size - the font size
      Returns:
      this element for method chaining
    • fontSizeRW

      public TextElement fontSizeRW(String size)
      Sets the font size with a unit (read-write).
      Parameters:
      size - the font size (e.g., "12px", "1em")
      Returns:
      this element for method chaining
    • fontWeight

      public TextElement fontWeight(TextElement.FontWeight weight)
      Sets the font weight.

      Uses write-only optimization. Use fontWeightRW(FontWeight) if you need to read the value back.

      Parameters:
      weight - the font weight
      Returns:
      this element for method chaining
    • fontWeightRW

      public TextElement fontWeightRW(TextElement.FontWeight weight)
      Sets the font weight (read-write).
      Parameters:
      weight - the font weight
      Returns:
      this element for method chaining
    • fontStyle

      public TextElement fontStyle(TextElement.FontStyle style)
      Sets the font style.

      Uses write-only optimization. Use fontStyleRW(FontStyle) if you need to read the value back.

      Parameters:
      style - the font style
      Returns:
      this element for method chaining
    • fontStyleRW

      public TextElement fontStyleRW(TextElement.FontStyle style)
      Sets the font style (read-write).
      Parameters:
      style - the font style
      Returns:
      this element for method chaining
    • textDecoration

      public TextElement textDecoration(TextElement.TextDecoration decoration)
      Sets the text decoration.

      Uses write-only optimization. Use textDecorationRW(TextDecoration) if you need to read the value back.

      Parameters:
      decoration - the text decoration
      Returns:
      this element for method chaining
    • textDecorationRW

      public TextElement textDecorationRW(TextElement.TextDecoration decoration)
      Sets the text decoration (read-write).
      Parameters:
      decoration - the text decoration
      Returns:
      this element for method chaining
    • letterSpacing

      public TextElement letterSpacing(double spacing)
      Sets the letter spacing.

      Uses write-only optimization. Use letterSpacingRW(double) if you need to read the value back.

      Parameters:
      spacing - the spacing value
      Returns:
      this element for method chaining
    • letterSpacing

      public TextElement letterSpacing(String spacing)
      Sets the letter spacing with a unit.

      Uses write-only optimization. Use letterSpacingRW(String) if you need to read the value back.

      Parameters:
      spacing - the spacing (e.g., "0.1em", "2px")
      Returns:
      this element for method chaining
    • letterSpacingRW

      public TextElement letterSpacingRW(double spacing)
      Sets the letter spacing (read-write).
      Parameters:
      spacing - the spacing value
      Returns:
      this element for method chaining
    • letterSpacingRW

      public TextElement letterSpacingRW(String spacing)
      Sets the letter spacing with a unit (read-write).
      Parameters:
      spacing - the spacing (e.g., "0.1em", "2px")
      Returns:
      this element for method chaining
    • wordSpacing

      public TextElement wordSpacing(double spacing)
      Sets the word spacing.

      Uses write-only optimization. Use wordSpacingRW(double) if you need to read the value back.

      Parameters:
      spacing - the spacing value
      Returns:
      this element for method chaining
    • wordSpacingRW

      public TextElement wordSpacingRW(double spacing)
      Sets the word spacing (read-write).
      Parameters:
      spacing - the spacing value
      Returns:
      this element for method chaining
    • add

      public TextElement add(SvgElement... children)
      Adds child elements to this text element.

      Typically used to add TSpanElement or TextPathElement children.

      Parameters:
      children - the child elements
      Returns:
      this element for method chaining