public class RichTextArea

  1. Object
  2. Component
  3. Container
  4. AbstractEditorComponent
  5. RichTextArea

ImplementsAnimation, Editable, EditorHost, Iterable<Component>, StyleListener

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).

On ports with low-level text input the editor uses the pure Codename One text engine (com.codename1.ui.editor), which renders the document itself and binds to the soft keyboard, hardware keyboard and IME. Ports without that contract use an editable BrowserComponent fallback. A platform port may transparently replace either 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));

Constructors

public RichTextArea()Creates an empty rich text editor.
public RichTextArea(String html)Creates a rich text editor initialized with the supplied HTML.

Methods

public void setHtml(String html)Replaces the entire editor content with the supplied HTML.
public void setContent(String content, RichTextFormat format)Replaces the editor content from any supported rich-text interchange format.
public void setRtf(String rtf)Replaces the editor content with RTF.
public void setMarkdown(String markdown)Replaces the editor content with Markdown.
public void setAsciiDoc(String asciidoc)Replaces the editor content with AsciiDoc.
public void getHtml(SuccessCallback<String> callback)Retrieves the current editor content as an HTML string.
public void getText(SuccessCallback<String> callback)Retrieves the current editor content as plain text (markup stripped).
public void getMarkdown(SuccessCallback<String> callback)Retrieves the current content as Markdown.
public void getAsciiDoc(SuccessCallback<String> callback)Retrieves the current content as AsciiDoc.
public void getRtf(SuccessCallback<String> callback)Retrieves the current content as RTF.
public void insertHtml(String html)Inserts the supplied HTML fragment at the current cursor position.
public void insertContent(String content, RichTextFormat format)Inserts content in any supported rich-text interchange format at the current selection.
public void insertRtf(String rtf)Inserts RTF at the current selection.
public void insertMarkdown(String markdown)Inserts Markdown at the current selection.
public void insertAsciiDoc(String asciidoc)Inserts AsciiDoc at the current selection.
public void insertImage(String url)Inserts an image at the current cursor position.
public void setPlaceholder(String text)Sets the placeholder text shown when the editor is empty.
public String getPlaceholder()Returns the current placeholder text.
public void bold()Toggles bold styling on the current selection.
public void italic()Toggles italic styling on the current selection.
public void underline()Toggles underline styling on the current selection.
public void strikeThrough()Toggles strike-through styling on the current selection.
public void insertOrderedList()Converts the current block(s) into an ordered (numbered) list.
public void insertUnorderedList()Converts the current block(s) into an unordered (bulleted) list.
public void indent()Increases the indentation of the current block.
public void outdent()Decreases the indentation of the current block.
public void justifyLeft()Left aligns the current block.
public void justifyCenter()Center aligns the current block.
public void justifyRight()Right aligns the current block.
public void createLink(String url)Wraps the current selection in a hyperlink.
public void removeLink()Removes the hyperlink covering the current selection.
public void setForegroundColor(int rgb)Sets the foreground (text) color of the current selection.
public void setHighlightColor(int rgb)Sets the highlight (background) color of the current selection.
public void setBlockFormat(String tag)Applies a block format / heading to the current block.
public void setFontSize(int size)Applies a relative font size (1 through 7, matching the legacy HTML font size scale) to the current selection.
public void removeFormat()Removes all inline formatting from the current selection.
public void undo()Undoes the last editing operation.
public void redo()Redoes the last undone editing operation.
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.

Inherited fields

Inherited methods

From Container

encloseIn, encloseIn, initLaf, getUIManager, setUIManager, isSurface, add, addAll, add, add, add, add, add, getLeadComponent, setLeadComponent, getLeadParent, keyPressed, keyReleased, getLayout, setLayout, invalidate, setShouldLayout, setShouldCalcPreferredSize, getLayoutWidth, getLayoutHeight, applyRTL, constrainWidthWhenScrollable, constrainHeightWhenScrollable, addComponent, addComponent, addComponent, addComponent, replaceAndWait, replaceAndWait, replace, replaceAndWait, replace, createReplaceTransition, isEnabled, setEnabled, removeComponent, cancelRepaints, flushReplace, removeAll, revalidateWithAnimationSafety, revalidate, revalidateLater, forceRevalidate, clearClientProperties, paint, paintGlass, layoutContainer, isSafeArea, setSafeArea, isSafeAreaRoot, getSafeAreaRoot, setSafeAreaRoot, getComponentCount, getComponentAt, getComponentIndex, contains, scrollComponentToVisible, getClosestComponentTo, getResponderAt, getComponentAt, findDropTargetAt, pointerPressed, calcPreferredSize, paramString, refreshTheme, isScrollableX, setScrollableX, isScrollableY, setScrollableY, getSideGap, getBottomGap, setScrollable, setCellRenderer, getScrollIncrement, setScrollIncrement, findFirstFocusable, dragInitiated, fireClicked, isSelectableInteraction, getGridPosY, paintComponentBackground, getGridPosX, animateHierarchyAndWait, createAnimateHierarchy, animateHierarchy, animateHierarchyFadeAndWait, createAnimateHierarchyFade, animateHierarchyFade, animateLayoutFadeAndWait, createAnimateLayoutFadeAndWait, animateLayoutFade, createAnimateLayoutFade, animateLayoutAndWait, animateLayout, updateTabIndices, createAnimateLayout, drop, createAnimateMotion, morph, morphAndWait, animateUnlayout, animateUnlayoutAndWait, createAnimateUnlayout, getChildrenAsList, iterator, iterator

From Component

setSameSize, isSetCursorSupported, parsePreferredSize, getDefaultDragTransparency, setDefaultDragTransparency, getEditingDelegate, setEditingDelegate, getCursor, setCursor, showNativeOverlay, hideNativeOverlay, updateNativeOverlay, getNativeOverlay, getAllStyles, getSameWidth, setSameWidth, getSameHeight, setSameHeight, getX, setX, getOuterX, getInnerX, getY, setY, getOuterY, getInnerY, isVisible, setVisible, getClientProperty, stripMarginAndPadding, putClientProperty, getDirtyRegion, setDirtyRegion, isOpaque, setOpaque, getWidth, setWidth, getOuterWidth, getInnerWidth, getHeight, setHeight, getOuterHeight, getInnerHeight, isDragRegion, getDragRegionStatus, getBaseline, getBaselineResizeBehavior, getPreferredSizeStr, setPreferredSizeStr, getPreferredSize, setPreferredSize, getScrollDimension, calcScrollSize, setScrollSize, getPreferredW, setPreferredW, getPreferredH, setPreferredH, getOuterPreferredH, getInnerPreferredH, getOuterPreferredW, getInnerPreferredW, setSize, getUIID, setUIID, setUIIDFinal, setUIID, getInlineAllStyles, setInlineAllStyles, getInlineSelectedStyles, setInlineSelectedStyles, getInlineUnselectedStyles, setInlineUnselectedStyles, getInlineDisabledStyles, setInlineDisabledStyles, getInlinePressedStyles, setInlinePressedStyles, remove, getParent, getOwner, setOwner, isOwnedBy, containsOrOwns, addFocusListener, removeFocusListener, addScrollListener, removeScrollListener, getSelectCommandText, setSelectCommandText, getLabelForComponent, setLabelForComponent, focusGained, focusLost, paintBackgrounds, paintShadows, getAbsoluteX, getAbsoluteY, isInClippingRegion, paintIntersectingComponentsAbove, paintScrollbars, paintScrollbarX, getScrollOpacity, getSelectedRect, paintScrollbarY, paintComponent, paintComponent, getBorder, getScrollable, paintBackground, isScrollable, getScrollX, setScrollX, getScrollY, setScrollY, onScrollX, onScrollY, getDraggedx, getDraggedy, contains, visibleBoundsContains, hasFixedPreferredSize, getBounds, getBounds, getVisibleBounds, getVisibleBounds, isFocusable, setFocusable, onSetFocusable, resetFocusable, getTabIndex, setTabIndex, getPreferredTabIndex, setPreferredTabIndex, isTraversable, setTraversable, handlesInput, setHandlesInput, consumesRawTextInput, hasFocus, setFocus, getComponentForm, getTopLevelContainer, repaint, repaint, longKeyPress, keyRepeated, registerForAnimation, deregisterFromAnimation, getAnimationManager, getScrollAnimationSpeed, setScrollAnimationSpeed, isBlockLead, setBlockLead, isIgnorePointerEvents, setIgnorePointerEvents, isRippleEffect, setRippleEffect, getInlineStylesTheme, setInlineStylesTheme, shouldRenderComponentSelection, isHideInLandscape, setHideInLandscape, createStyleAnimation, isSmoothScrolling, setSmoothScrolling, pointerHover, stopScrollMomentum, pointerHoverReleased, pointerHoverPressed, pinch, pinchReleased, pinch, rotation, isPinchBlocksDragAndDrop, setPinchBlocksDragAndDrop, pointerDragged, getDragImage, getDragTransparency, setDragTransparency, toImage, drawDraggedImage, draggingOver, dragEnter, dragExit, addPullToRefresh, setPullToRefresh, respondsToPointerEvents, pointerDragged, isStickyDrag, pointerPressed, isDragAndDropOperation, pointerReleased, longPointerPress, pointerReleased, setVerticalScrollBounds, setHorizontalScrollBounds, isVScrollThumbGrabbed, isHScrollThumbGrabbed, isVScrollThumbHover, isHScrollThumbHover, isTensileDragEnabled, setTensileDragEnabled, getTextSelectionSupport, addDropListener, removeDropListener, addDragOverListener, removeDragOverListener, isNativeDragSource, setNativeDragSource, getNativeDragOperation, setNativeDragOperation, createNativeDragOperation, isNativeDropTarget, setNativeDropTarget, getAcceptedDropMimeTypes, setAcceptedDropMimeTypes, getAcceptedDropActions, setAcceptedDropActions, canAcceptNativeDrop, nativeDragEnter, nativeDragOver, nativeDragExit, nativeDrop, addNativeDropListener, removeNativeDropListener, addNativeDragOverListener, removeNativeDragOverListener, dragFinished, addDragFinishedListener, addStateChangeListener, removeStateChangeListener, addPointerPressedListener, addLongPressListener, addContextMenuListener, removeContextMenuListener, addMouseWheelListener, removeMouseWheelListener, addStylusListener, removeStylusListener, mouseWheel, paintRippleOverlay, removePointerPressedListener, removeLongPressListener, removeDragFinishedListener, addPointerReleasedListener, removePointerReleasedListener, addPointerDraggedListener, removePointerDraggedListener, getDragSpeed, getStyle, getPressedStyle, setPressedStyle, initUnselectedStyle, initPressedStyle, initDisabledStyle, initSelectedStyle, getUnselectedStyle, setUnselectedStyle, getSelectedStyle, setSelectedStyle, getDisabledStyle, setDisabledStyle, installDefaultPainter, requestFocus, toString, refreshTheme, refreshTheme, isDragActivated, animate, scrollRectToVisible, scrollRectToVisible, paintBorder, paintBorderBackground, isCellRenderer, isScrollVisible, setScrollVisible, setIsScrollVisible, startEditingAsync, stopEditing, isEditing, laidOut, deinitialize, initComponent, isInitialized, setInitialized, styleChanged, getNextFocusDown, setNextFocusDown, getNextFocusUp, setNextFocusUp, getNextFocusLeft, setNextFocusLeft, getNextFocusRight, setNextFocusRight, getName, setName, initCustomStyle, deinitializeCustomStyle, isRTL, setRTL, isTactileTouch, isTactileTouch, setTactileTouch, getPropertyNames, getPropertyTypes, getPropertyTypeNames, getPropertyValue, setPropertyValue, paintLockRelease, paintLock, isSnapToGrid, setSnapToGrid, shouldBlockSideSwipe, shouldBlockSideSwipeLeft, shouldBlockSideSwipeRight, blocksSideSwipe, isFlatten, setFlatten, getTensileLength, setTensileLength, isGrabsPointerEvents, setGrabsPointerEvents, getScrollOpacityChangeSpeed, setScrollOpacityChangeSpeed, growShrink, isAlwaysTensile, setAlwaysTensile, isDraggable, setDraggable, isDropTarget, setDropTarget, isChildOf, isHideInPortrait, setHideInPortrait, getBindablePropertyNames, getBindablePropertyTypes, bindProperty, unbindProperty, getBoundPropertyValue, setBoundPropertyValue, getCloudBoundProperty, setCloudBoundProperty, getCloudDestinationProperty, setCloudDestinationProperty, getComponentState, setComponentState, setHidden, isHidden, setHidden, isHidden, announceForAccessibility, getAccessibilityText, setAccessibilityText, getSemantics, getAccessibilityNode, accessibilityChanged, accessibilityChanged, getTooltip, setTooltip

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 String
the initial HTML content

Method details

setHtml

public void setHtml(String html)
Replaces the entire editor content with the supplied HTML.

Parameters

html String
the HTML to display and edit

setContent

public void setContent(String content, RichTextFormat format)
Replaces the editor content from any supported rich-text interchange format.

setRtf

public void setRtf(String rtf)
Replaces the editor content with RTF.

setMarkdown

public void setMarkdown(String markdown)
Replaces the editor content with Markdown.

setAsciiDoc

public void setAsciiDoc(String asciidoc)
Replaces the editor content with AsciiDoc.

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 SuccessCallback<String>
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 SuccessCallback<String>
receives the plain text content

getMarkdown

public void getMarkdown(SuccessCallback<String> callback)
Retrieves the current content as Markdown.

getAsciiDoc

public void getAsciiDoc(SuccessCallback<String> callback)
Retrieves the current content as AsciiDoc.

getRtf

public void getRtf(SuccessCallback<String> callback)
Retrieves the current content as RTF.

insertHtml

public void insertHtml(String html)
Inserts the supplied HTML fragment at the current cursor position.

Parameters

html String
the HTML fragment to insert

insertContent

public void insertContent(String content, RichTextFormat format)
Inserts content in any supported rich-text interchange format at the current selection.

insertRtf

public void insertRtf(String rtf)
Inserts RTF at the current selection.

insertMarkdown

public void insertMarkdown(String markdown)
Inserts Markdown at the current selection.

insertAsciiDoc

public void insertAsciiDoc(String asciidoc)
Inserts AsciiDoc at the current selection.

insertImage

public void insertImage(String url)
Inserts an image at the current cursor position.

Parameters

url String
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 String
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 String
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 int
the color as a 0xRRGGBB integer

setHighlightColor

public void setHighlightColor(int rgb)
Sets the highlight (background) color of the current selection.

Parameters

rgb int
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 String
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 int
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 String
the command name to test
callback SuccessCallback<Boolean>
receives true if the command is active for the current selection