Class RichTextArea
- All Implemented Interfaces:
Animation, Editable, EditorHost, StyleListener, Iterable<Component>
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));
-
Field Summary
Fields inherited from class AbstractEditorComponent
BACKEND_BROWSER, BACKEND_PUREFields inherited from class Component
BASELINE, BOTTOM, BRB_CENTER_OFFSET, BRB_CONSTANT_ASCENT, BRB_CONSTANT_DESCENT, BRB_OTHER, CENTER, CROSSHAIR_CURSOR, DEFAULT_CURSOR, DRAG_REGION_IMMEDIATELY_DRAG_X, DRAG_REGION_IMMEDIATELY_DRAG_XY, DRAG_REGION_IMMEDIATELY_DRAG_Y, DRAG_REGION_LIKELY_DRAG_X, DRAG_REGION_LIKELY_DRAG_XY, DRAG_REGION_LIKELY_DRAG_Y, DRAG_REGION_NOT_DRAGGABLE, DRAG_REGION_POSSIBLE_DRAG_X, DRAG_REGION_POSSIBLE_DRAG_XY, DRAG_REGION_POSSIBLE_DRAG_Y, E_RESIZE_CURSOR, HAND_CURSOR, LEFT, MOVE_CURSOR, N_RESIZE_CURSOR, NE_RESIZE_CURSOR, NW_RESIZE_CURSOR, RIGHT, S_RESIZE_CURSOR, SE_RESIZE_CURSOR, SW_RESIZE_CURSOR, TEXT_CURSOR, TOP, W_RESIZE_CURSOR, WAIT_CURSOR -
Constructor Summary
ConstructorsConstructorDescriptionCreates an empty rich text editor.RichTextArea(String html) Creates a rich text editor initialized with the supplied HTML. -
Method Summary
Modifier and TypeMethodDescriptionvoidbold()Toggles bold styling on the current selection.voidcreateLink(String url) Wraps the current selection in a hyperlink.voidgetAsciiDoc(SuccessCallback<String> callback) Retrieves the current content as AsciiDoc.voidgetHtml(SuccessCallback<String> callback) Retrieves the current editor content as an HTML string.voidgetMarkdown(SuccessCallback<String> callback) Retrieves the current content as Markdown.Returns the current placeholder text.voidgetRtf(SuccessCallback<String> callback) Retrieves the current content as RTF.voidgetText(SuccessCallback<String> callback) Retrieves the current editor content as plain text (markup stripped).voidindent()Increases the indentation of the current block.voidinsertAsciiDoc(String asciidoc) Inserts AsciiDoc at the current selection.voidinsertContent(String content, RichTextFormat format) Inserts content in any supported rich-text interchange format at the current selection.voidinsertHtml(String html) Inserts the supplied HTML fragment at the current cursor position.voidinsertImage(String url) Inserts an image at the current cursor position.voidinsertMarkdown(String markdown) Inserts Markdown at the current selection.voidConverts the current block(s) into an ordered (numbered) list.voidInserts RTF at the current selection.voidConverts the current block(s) into an unordered (bulleted) list.voiditalic()Toggles italic styling on the current selection.voidCenter aligns the current block.voidLeft aligns the current block.voidRight aligns the current block.voidoutdent()Decreases the indentation of the current block.voidqueryCommandState(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.voidredo()Redoes the last undone editing operation.voidRemoves all inline formatting from the current selection.voidRemoves the hyperlink covering the current selection.voidsetAsciiDoc(String asciidoc) Replaces the editor content with AsciiDoc.voidsetBlockFormat(String tag) Applies a block format / heading to the current block.voidsetContent(String content, RichTextFormat format) Replaces the editor content from any supported rich-text interchange format.voidsetFontSize(int size) Applies a relative font size (1 through 7, matching the legacy HTML font size scale) to the current selection.voidsetForegroundColor(int rgb) Sets the foreground (text) color of the current selection.voidsetHighlightColor(int rgb) Sets the highlight (background) color of the current selection.voidReplaces the entire editor content with the supplied HTML.voidsetMarkdown(String markdown) Replaces the editor content with Markdown.voidsetPlaceholder(String text) Sets the placeholder text shown when the editor is empty.voidReplaces the editor content with RTF.voidToggles strike-through styling on the current selection.voidToggles underline styling on the current selection.voidundo()Undoes the last editing operation.Methods inherited from class AbstractEditorComponent
addChangeListener, addReadyListener, blurEditor, command, editorChanged, fireEditorEvent, focusEditor, getDefaultBackend, getInternalBrowser, isEditable, isEditorReady, isNativeEditor, isTextInputSupported, onReady, query, removeChangeListener, removeReadyListener, setDefaultBackend, setEditable, startTextInput, stopTextInput, updateTextInputStateMethods inherited from class Container
add, add, add, add, add, add, addAll, addComponent, addComponent, addComponent, addComponent, animateHierarchy, animateHierarchyAndWait, animateHierarchyFade, animateHierarchyFadeAndWait, animateLayout, animateLayoutAndWait, animateLayoutFade, animateLayoutFadeAndWait, animateUnlayout, animateUnlayoutAndWait, applyRTL, calcPreferredSize, cancelRepaints, clearClientProperties, constrainHeightWhenScrollable, constrainWidthWhenScrollable, contains, createAnimateHierarchy, createAnimateHierarchyFade, createAnimateLayout, createAnimateLayoutFade, createAnimateLayoutFadeAndWait, createAnimateMotion, createAnimateUnlayout, createReplaceTransition, dragInitiated, drop, encloseIn, encloseIn, findDropTargetAt, findFirstFocusable, fireClicked, flushReplace, forceRevalidate, getBottomGap, getChildrenAsList, getClosestComponentTo, getComponentAt, getComponentAt, getComponentCount, getComponentIndex, getGridPosX, getGridPosY, getLayout, getLayoutHeight, getLayoutWidth, getLeadComponent, getLeadParent, getResponderAt, getSafeAreaRoot, getScrollIncrement, getSideGap, getUIManager, initLaf, invalidate, isEnabled, isSafeArea, isSafeAreaRoot, isScrollableX, isScrollableY, isSelectableInteraction, isSurface, iterator, iterator, keyPressed, keyReleased, layoutContainer, morph, morphAndWait, paint, paintComponentBackground, paintGlass, paramString, pointerPressed, refreshTheme, removeAll, removeComponent, replace, replace, replaceAndWait, replaceAndWait, replaceAndWait, revalidate, revalidateLater, revalidateWithAnimationSafety, scrollComponentToVisible, setCellRenderer, setEnabled, setLayout, setLeadComponent, setSafeArea, setSafeAreaRoot, setScrollable, setScrollableX, setScrollableY, setScrollIncrement, setShouldCalcPreferredSize, setShouldLayout, setUIManager, updateTabIndicesMethods inherited from class Component
accessibilityChanged, accessibilityChanged, addContextMenuListener, addDragFinishedListener, addDragOverListener, addDropListener, addFocusListener, addLongPressListener, addMouseWheelListener, addPointerDraggedListener, addPointerPressedListener, addPointerReleasedListener, addPullToRefresh, addScrollListener, addStateChangeListener, addStylusListener, animate, announceForAccessibility, bindProperty, blocksSideSwipe, calcScrollSize, contains, containsOrOwns, createStyleAnimation, deinitialize, deinitializeCustomStyle, dragEnter, dragExit, dragFinished, draggingOver, drawDraggedImage, focusGained, focusLost, getAbsoluteX, getAbsoluteY, getAccessibilityNode, getAccessibilityText, getAllStyles, getAnimationManager, getBaseline, getBaselineResizeBehavior, getBindablePropertyNames, getBindablePropertyTypes, getBorder, getBoundPropertyValue, getBounds, getBounds, getClientProperty, getCloudBoundProperty, getCloudDestinationProperty, getComponentForm, getComponentState, getCursor, getDefaultDragTransparency, getDirtyRegion, getDisabledStyle, getDraggedx, getDraggedy, getDragImage, getDragRegionStatus, getDragSpeed, getDragTransparency, getEditingDelegate, getHeight, getInlineAllStyles, getInlineDisabledStyles, getInlinePressedStyles, getInlineSelectedStyles, getInlineStylesTheme, getInlineUnselectedStyles, getInnerHeight, getInnerPreferredH, getInnerPreferredW, getInnerWidth, getInnerX, getInnerY, getLabelForComponent, getName, getNativeOverlay, getNextFocusDown, getNextFocusLeft, getNextFocusRight, getNextFocusUp, getOuterHeight, getOuterPreferredH, getOuterPreferredW, getOuterWidth, getOuterX, getOuterY, getOwner, getParent, getPreferredH, getPreferredSize, getPreferredSizeStr, getPreferredTabIndex, getPreferredW, getPressedStyle, getPropertyNames, getPropertyTypeNames, getPropertyTypes, getPropertyValue, getSameHeight, getSameWidth, getScrollable, getScrollAnimationSpeed, getScrollDimension, getScrollOpacity, getScrollOpacityChangeSpeed, getScrollX, getScrollY, getSelectCommandText, getSelectedRect, getSelectedStyle, getSemantics, getStyle, getTabIndex, getTensileLength, getTextSelectionSupport, getTooltip, getUIID, getUnselectedStyle, getVisibleBounds, getVisibleBounds, getWidth, getX, getY, growShrink, handlesInput, hasFixedPreferredSize, hasFocus, hideNativeOverlay, initComponent, initCustomStyle, initDisabledStyle, initPressedStyle, initSelectedStyle, initUnselectedStyle, installDefaultPainter, isAlwaysTensile, isBlockLead, isCellRenderer, isChildOf, isDragActivated, isDragAndDropOperation, isDraggable, isDragRegion, isDropTarget, isEditing, isFlatten, isFocusable, isGrabsPointerEvents, isHidden, isHidden, isHideInLandscape, isHideInPortrait, isHScrollThumbGrabbed, isHScrollThumbHover, isIgnorePointerEvents, isInClippingRegion, isInitialized, isOpaque, isOwnedBy, isPinchBlocksDragAndDrop, isRippleEffect, isRTL, isScrollable, isScrollVisible, isSetCursorSupported, isSmoothScrolling, isSnapToGrid, isStickyDrag, isTactileTouch, isTactileTouch, isTensileDragEnabled, isTraversable, isVisible, isVScrollThumbGrabbed, isVScrollThumbHover, keyRepeated, laidOut, longKeyPress, longPointerPress, onScrollX, onScrollY, onSetFocusable, paintBackground, paintBackgrounds, paintBorder, paintBorderBackground, paintComponent, paintComponent, paintIntersectingComponentsAbove, paintLock, paintLockRelease, paintRippleOverlay, paintScrollbars, paintScrollbarX, paintScrollbarY, paintShadows, parsePreferredSize, pinch, pinch, pinchReleased, pointerDragged, pointerDragged, pointerHover, pointerHoverPressed, pointerHoverReleased, pointerPressed, pointerReleased, pointerReleased, putClientProperty, refreshTheme, refreshTheme, remove, removeContextMenuListener, removeDragFinishedListener, removeDragOverListener, removeDropListener, removeFocusListener, removeLongPressListener, removeMouseWheelListener, removePointerDraggedListener, removePointerPressedListener, removePointerReleasedListener, removeScrollListener, removeStateChangeListener, removeStylusListener, repaint, repaint, requestFocus, resetFocusable, respondsToPointerEvents, rotation, scrollRectToVisible, scrollRectToVisible, setAccessibilityText, setAlwaysTensile, setBlockLead, setBoundPropertyValue, setCloudBoundProperty, setCloudDestinationProperty, setComponentState, setCursor, setDefaultDragTransparency, setDirtyRegion, setDisabledStyle, setDraggable, setDragTransparency, setDropTarget, setEditingDelegate, setFlatten, setFocus, setFocusable, setGrabsPointerEvents, setHandlesInput, setHeight, setHidden, setHidden, setHideInLandscape, setHideInPortrait, setHorizontalScrollBounds, setIgnorePointerEvents, setInitialized, setInlineAllStyles, setInlineDisabledStyles, setInlinePressedStyles, setInlineSelectedStyles, setInlineStylesTheme, setInlineUnselectedStyles, setIsScrollVisible, setLabelForComponent, setName, setNextFocusDown, setNextFocusLeft, setNextFocusRight, setNextFocusUp, setOpaque, setOwner, setPinchBlocksDragAndDrop, setPreferredH, setPreferredSize, setPreferredSizeStr, setPreferredTabIndex, setPreferredW, setPressedStyle, setPropertyValue, setPullToRefresh, setRippleEffect, setRTL, setSameHeight, setSameSize, setSameWidth, setScrollAnimationSpeed, setScrollOpacityChangeSpeed, setScrollSize, setScrollVisible, setScrollX, setScrollY, setSelectCommandText, setSelectedStyle, setSize, setSmoothScrolling, setSnapToGrid, setTabIndex, setTactileTouch, setTensileDragEnabled, setTensileLength, setTooltip, setTraversable, setUIID, setUIID, setUIIDFinal, setUnselectedStyle, setVerticalScrollBounds, setVisible, setWidth, setX, setY, shouldBlockSideSwipe, shouldBlockSideSwipeLeft, shouldBlockSideSwipeRight, shouldRenderComponentSelection, showNativeOverlay, startEditingAsync, stopEditing, stripMarginAndPadding, styleChanged, toImage, toString, unbindProperty, updateNativeOverlay, visibleBoundsContains
-
Constructor Details
-
RichTextArea
public RichTextArea()Creates an empty rich text editor. -
RichTextArea
Creates a rich text editor initialized with the supplied HTML.
Parameters
html: the initial HTML content
-
-
Method Details
-
setHtml
Replaces the entire editor content with the supplied HTML.
Parameters
html: the HTML to display and edit
-
setContent
Replaces the editor content from any supported rich-text interchange format. -
setRtf
Replaces the editor content with RTF. -
setMarkdown
Replaces the editor content with Markdown. -
setAsciiDoc
Replaces the editor content with AsciiDoc. -
getHtml
Retrieves the current editor content as an HTML string. The callback is invoked on the EDT.
Parameters
callback: receives the HTML content
-
getText
Retrieves the current editor content as plain text (markup stripped). The callback is invoked on the EDT.
Parameters
callback: receives the plain text content
-
getMarkdown
Retrieves the current content as Markdown. -
getAsciiDoc
Retrieves the current content as AsciiDoc. -
getRtf
Retrieves the current content as RTF. -
insertHtml
Inserts the supplied HTML fragment at the current cursor position.
Parameters
html: the HTML fragment to insert
-
insertContent
Inserts content in any supported rich-text interchange format at the current selection. -
insertRtf
Inserts RTF at the current selection. -
insertMarkdown
Inserts Markdown at the current selection. -
insertAsciiDoc
Inserts AsciiDoc at the current selection. -
insertImage
Inserts an image at the current cursor position.
Parameters
url: the image URL (may be an http(s) URL or a data: URI)
-
setPlaceholder
Sets the placeholder text shown when the editor is empty.
Parameters
text: the placeholder hint
-
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
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
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
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
-
-