Class RichTextArea

All Implemented Interfaces:
Animation, Editable, StyleListener, Iterable<Component>

public class RichTextArea extends AbstractEditorComponent

A native visual editor for rich text / HTML content (a WYSIWYG editor).

RichTextArea lets the user visually edit formatted text - bold, italic, lists, links, colors, headings and more - and exchange the result as HTML with your application. It works on phones and tablets (with the on screen virtual keyboard) as well as on desktops (with a physical keyboard).

By default the editor is implemented as a contenteditable surface hosted inside the platform's native web widget (BrowserComponent), which makes it 100% cross platform and gives correct keyboard, selection and IME behavior on every device for free. A platform port may transparently replace this with a fully native editing widget; see com.codename1.impl.CodenameOneImplementation#createNativeEditorPeer(Object, String).

Basic usage
Form hi = new Form("Rich Text", new BorderLayout());
RichTextArea editor = new RichTextArea();
editor.setHtml("<p>Hello <b>world</b></p>");

Toolbar tb = hi.getToolbar();
tb.addCommandToRightBar("B", null, e -> editor.bold());
tb.addCommandToRightBar("I", null, e -> editor.italic());

hi.add(BorderLayout.CENTER, editor);
hi.show();

// later, read the edited content back:
editor.getHtml(html -> Log.p("User wrote: " + html));
  • Constructor Details

    • RichTextArea

      public RichTextArea()
      Creates an empty rich text editor.
    • RichTextArea

      public RichTextArea(String html)

      Creates a rich text editor initialized with the supplied HTML.

      Parameters
      • html: the initial HTML content
  • Method Details

    • setHtml

      public void setHtml(String html)

      Replaces the entire editor content with the supplied HTML.

      Parameters
      • html: the HTML to display and edit
    • getHtml

      public void getHtml(SuccessCallback<String> callback)

      Retrieves the current editor content as an HTML string. The callback is invoked on the EDT.

      Parameters
      • callback: receives the HTML content
    • getText

      public void getText(SuccessCallback<String> callback)

      Retrieves the current editor content as plain text (markup stripped). The callback is invoked on the EDT.

      Parameters
      • callback: receives the plain text content
    • insertHtml

      public void insertHtml(String html)

      Inserts the supplied HTML fragment at the current cursor position.

      Parameters
      • html: the HTML fragment to insert
    • insertImage

      public void insertImage(String url)

      Inserts an image at the current cursor position.

      Parameters
      • url: the image URL (may be an http(s) URL or a data: URI)
    • setPlaceholder

      public void setPlaceholder(String text)

      Sets the placeholder text shown when the editor is empty.

      Parameters
      • text: the placeholder hint
    • getPlaceholder

      public String getPlaceholder()
      Returns the current placeholder text.
    • bold

      public void bold()
      Toggles bold styling on the current selection.
    • italic

      public void italic()
      Toggles italic styling on the current selection.
    • underline

      public void underline()
      Toggles underline styling on the current selection.
    • strikeThrough

      public void strikeThrough()
      Toggles strike-through styling on the current selection.
    • insertOrderedList

      public void insertOrderedList()
      Converts the current block(s) into an ordered (numbered) list.
    • insertUnorderedList

      public void insertUnorderedList()
      Converts the current block(s) into an unordered (bulleted) list.
    • indent

      public void indent()
      Increases the indentation of the current block.
    • outdent

      public void outdent()
      Decreases the indentation of the current block.
    • justifyLeft

      public void justifyLeft()
      Left aligns the current block.
    • justifyCenter

      public void justifyCenter()
      Center aligns the current block.
    • justifyRight

      public void justifyRight()
      Right aligns the current block.
    • createLink

      public void createLink(String url)

      Wraps the current selection in a hyperlink.

      Parameters
      • url: the link target
    • removeLink

      public void removeLink()
      Removes the hyperlink covering the current selection.
    • setForegroundColor

      public void setForegroundColor(int rgb)

      Sets the foreground (text) color of the current selection.

      Parameters
      • rgb: the color as a 0xRRGGBB integer
    • setHighlightColor

      public void setHighlightColor(int rgb)

      Sets the highlight (background) color of the current selection.

      Parameters
      • rgb: the color as a 0xRRGGBB integer
    • setBlockFormat

      public void setBlockFormat(String tag)

      Applies a block format / heading to the current block. Common values are "p", "h1" .. "h6", "pre" and "blockquote".

      Parameters
      • tag: the block tag name
    • setFontSize

      public void setFontSize(int size)

      Applies a relative font size (1 through 7, matching the legacy HTML font size scale) to the current selection.

      Parameters
      • size: a value between 1 and 7
    • removeFormat

      public void removeFormat()
      Removes all inline formatting from the current selection.
    • undo

      public void undo()
      Undoes the last editing operation.
    • redo

      public void redo()
      Redoes the last undone editing operation.
    • queryCommandState

      public void queryCommandState(String command, SuccessCallback<Boolean> callback)

      Queries whether a given inline command (e.g. "bold", "italic", "underline") is currently active for the selection, which is useful to keep a formatting toolbar in sync. The callback is invoked on the EDT.

      Parameters
      • command: the command name to test

      • callback: receives true if the command is active for the current selection