Class RichTextComponent

java.lang.Object
com.codename1.ui.Component
com.codename1.ui.RichTextComponent
All Implemented Interfaces:
Animation, Editable, StyleListener

public class RichTextComponent extends Component

A read-only component that renders multi-styled, word-wrapped rich text: headings, bold / italic / underline / strike, inline code, colored and highlighted spans, per-paragraph alignment and indentation, ordered and unordered lists, block quotes, preformatted blocks, inline images and tappable hyperlinks. Content is supplied as HTML, Markdown, AsciiDoc, RTF or plain text, or built up programmatically from styled runs.

Unlike a single-style Label/SpanLabel it keeps a distinct style per character, and unlike a full BrowserComponent it is a lightweight native component that measures and paints text directly, so it embeds cleanly inside ordinary layouts and reports an accurate height-for-width preferred size. It is the read-only counterpart to the rich text editor: both share the same content model and RichRunPainter styling, so styled text rendered by one matches the other.

Typical use:


RichTextComponent rt = new RichTextComponent();
rt.setMarkdown("# Title\n\nSome **bold** and *italic* text with a [link](https://codenameone.com).");
rt.addLinkListener(e -> CN.execute((String) e.getSource()));
form.add(rt);

The default RichTextComponent.SizeMode.SHRINK makes the component as tall as its wrapped content at the width it is given (ideal inside a scrollable Form); RichTextComponent.SizeMode.SCROLL keeps the component at the size its parent assigns and scrolls its content vertically.

  • Constructor Details

    • RichTextComponent

      public RichTextComponent()
      Creates an empty rich text component.
    • RichTextComponent

      public RichTextComponent(String plainText)
      Creates a rich text component showing the given plain text.
      Parameters:
      plainText - the text
  • Method Details

    • setHtml

      public RichTextComponent setHtml(String html)
      Replaces the content with the given HTML fragment.
      Parameters:
      html - the HTML markup
      Returns:
      this, for chaining
    • setMarkdown

      public RichTextComponent setMarkdown(String markdown)
      Replaces the content with the given Markdown source.
      Parameters:
      markdown - the Markdown source
      Returns:
      this, for chaining
    • setContent

      public RichTextComponent setContent(String content, RichTextFormat format)
      Replaces the content parsed from the given format.
      Parameters:
      content - the source content
      format - the format the content is written in
      Returns:
      this, for chaining
    • setText

      public RichTextComponent setText(String plainText)
      Replaces the content with unstyled plain text. Newlines become paragraph breaks.
      Parameters:
      plainText - the text
      Returns:
      this, for chaining
    • clear

      public RichTextComponent clear()
      Removes all content, leaving the component empty. Useful before rebuilding content run by run with append(String, TextStyle).
      Returns:
      this, for chaining
    • append

      public RichTextComponent append(String runText, TextStyle style)
      Appends a run of text in the given style to the current content (builder style). A run may contain newlines to start new paragraphs.
      Parameters:
      runText - the run text
      style - the run style, or null for the default style
      Returns:
      this, for chaining
    • append

      public RichTextComponent append(String runText, TextStyle style, String link)
      Appends a run of styled text that acts as a hyperlink.
      Parameters:
      runText - the run text
      style - the run style, or null for the default style
      link - the hyperlink target reported to link listeners, or null for none
      Returns:
      this, for chaining
    • getText

      public String getText()
      The plain text of the current content (styling and structure removed).
      Returns:
      the plain text
    • setTextAlign

      public RichTextComponent setTextAlign(int align)
      Overrides the horizontal alignment of every paragraph, ignoring any alignment carried by the markup. Pass one of Component.LEFT, Component.CENTER, Component.RIGHT, or -1 to restore per-paragraph alignment from the content.
      Parameters:
      align - the alignment constant, or -1 to clear the override
      Returns:
      this, for chaining
    • preferredSizeForWidth

      public Dimension preferredSizeForWidth(int width)
      Lays the content out at the given outer width and returns the resulting preferred size (including this component's padding). This is a stateless height-for-width query useful for custom layout managers that need the wrapped size before assigning bounds.
      Parameters:
      width - the outer width available to the component
      Returns:
      the preferred size at that width
    • setSizeMode

      public RichTextComponent setSizeMode(RichTextComponent.SizeMode mode)
      Sets the sizing behavior. Defaults to RichTextComponent.SizeMode.SHRINK.
      Parameters:
      mode - the size mode
      Returns:
      this, for chaining
    • getSizeMode

      public RichTextComponent.SizeMode getSizeMode()
      The current size mode.
      Returns:
      the size mode
    • isScrollableY

      public boolean isScrollableY()
      Description copied from class: Component

      Indicates whether the component should/could scroll on the Y axis

      Returns

      whether the component is scrollable on the X axis

      Overrides:
      isScrollableY in class Component
    • setLinkColor

      public RichTextComponent setLinkColor(int rgb)
      Sets the color (0xRRGGBB) used for hyperlink text that does not carry an explicit color.
      Parameters:
      rgb - the link color
      Returns:
      this, for chaining
    • setImageResolver

      public RichTextComponent setImageResolver(RichTextComponent.ImageResolver resolver)
      Sets the resolver used to load inline images from their source strings.
      Parameters:
      resolver - the image resolver, or null to render placeholders
      Returns:
      this, for chaining
    • addLinkListener

      public void addLinkListener(ActionListener l)
      Adds a listener notified when a hyperlink is tapped. The ActionEvent.getSource() is the link target string.
      Parameters:
      l - the listener
    • removeLinkListener

      public void removeLinkListener(ActionListener l)
      Removes a previously added link listener.
      Parameters:
      l - the listener
    • setWidth

      public void setWidth(int width)
      Description copied from class: Component

      Sets the Component width, this method is exposed for the purpose of external layout managers and should not be invoked directly.

      If a user wishes to affect the component size, setPreferredSize should be used.

      Parameters
      • width: the width of the component
      See also
      • #setPreferredSize
      Overrides:
      setWidth in class Component
    • calcPreferredSize

      protected Dimension calcPreferredSize()
      Description copied from class: Component

      Calculates the preferred size based on component content. This method is invoked lazily by getPreferred size.

      Returns

      the calculated preferred size based on component content

      Overrides:
      calcPreferredSize in class Component
    • calcScrollSize

      protected Dimension calcScrollSize()
      Description copied from class: Component

      Method that can be overriden to represent the actual size of the component when it differs from the desireable size for the viewport

      Returns

      scroll size, by default this is the same as the preferred size

      Overrides:
      calcScrollSize in class Component
    • paint

      public void paint(Graphics g)
      Description copied from class: Component

      This method paints the Component on the screen, it should be overriden by subclasses to perform custom drawing or invoke the UI API's to let the PLAF perform the rendering.

      Parameters
      • g: the component graphics
      Specified by:
      paint in interface Animation
      Overrides:
      paint in class Component
    • pointerReleased

      public void pointerReleased(int x, int y)
      Description copied from class: Component

      If this Component is focused, the pointer released event will call this method

      Parameters
      • x: the pointer x coordinate

      • y: the pointer y coordinate

      Overrides:
      pointerReleased in class Component