public class RichTextArea
- Object
- Component
- Container
- AbstractEditorComponent
- 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
From Component
DEFAULT_CURSOR, CROSSHAIR_CURSOR, TEXT_CURSOR, WAIT_CURSOR, SW_RESIZE_CURSOR, SE_RESIZE_CURSOR, NW_RESIZE_CURSOR, NE_RESIZE_CURSOR, N_RESIZE_CURSOR, S_RESIZE_CURSOR, W_RESIZE_CURSOR, E_RESIZE_CURSOR, HAND_CURSOR, MOVE_CURSOR, DRAG_REGION_NOT_DRAGGABLE, DRAG_REGION_POSSIBLE_DRAG_X, DRAG_REGION_POSSIBLE_DRAG_Y, DRAG_REGION_POSSIBLE_DRAG_XY, DRAG_REGION_LIKELY_DRAG_X, DRAG_REGION_LIKELY_DRAG_Y, DRAG_REGION_LIKELY_DRAG_XY, DRAG_REGION_IMMEDIATELY_DRAG_X, DRAG_REGION_IMMEDIATELY_DRAG_Y, DRAG_REGION_IMMEDIATELY_DRAG_XY, BRB_CONSTANT_ASCENT, BRB_CONSTANT_DESCENT, BRB_CENTER_OFFSET, BRB_OTHER, CENTER, TOP, LEFT, BOTTOM, RIGHT, BASELINE
Inherited methods
From AbstractEditorComponent
fireEditorEvent, command, query, onReady, isEditorReady, isNativeEditor, addChangeListener, removeChangeListener, addReadyListener, removeReadyListener, setEditable, isEditable, focusEditor, blurEditor, isTextInputSupported, startTextInput, updateTextInputState, stopTextInput, editorChanged
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()RichTextArea
public RichTextArea(String html)Parameters
htmlString- the initial HTML content
Method details
setHtml
public void setHtml(String html)Parameters
htmlString- the HTML to display and edit
setContent
public void setContent(String content, RichTextFormat format)setRtf
public void setRtf(String rtf)setMarkdown
public void setMarkdown(String markdown)setAsciiDoc
public void setAsciiDoc(String asciidoc)getHtml
public void getHtml(SuccessCallback<String> callback)Parameters
callbackSuccessCallback<String>- receives the HTML content
getText
public void getText(SuccessCallback<String> callback)Parameters
callbackSuccessCallback<String>- receives the plain text content
getMarkdown
public void getMarkdown(SuccessCallback<String> callback)getAsciiDoc
public void getAsciiDoc(SuccessCallback<String> callback)getRtf
public void getRtf(SuccessCallback<String> callback)insertHtml
public void insertHtml(String html)Parameters
htmlString- the HTML fragment to insert
insertContent
public void insertContent(String content, RichTextFormat format)insertRtf
public void insertRtf(String rtf)insertMarkdown
public void insertMarkdown(String markdown)insertAsciiDoc
public void insertAsciiDoc(String asciidoc)insertImage
public void insertImage(String url)Parameters
urlString- the image URL (may be an http(s) URL or a data: URI)
setPlaceholder
public void setPlaceholder(String text)Parameters
textString- the placeholder hint
getPlaceholder
public String getPlaceholder()bold
public void bold()italic
public void italic()underline
public void underline()strikeThrough
public void strikeThrough()insertOrderedList
public void insertOrderedList()insertUnorderedList
public void insertUnorderedList()indent
public void indent()outdent
public void outdent()justifyLeft
public void justifyLeft()justifyCenter
public void justifyCenter()justifyRight
public void justifyRight()createLink
public void createLink(String url)Parameters
urlString- the link target
removeLink
public void removeLink()setForegroundColor
public void setForegroundColor(int rgb)Parameters
rgbint- the color as a 0xRRGGBB integer
setHighlightColor
public void setHighlightColor(int rgb)Parameters
rgbint- the color as a 0xRRGGBB integer
setBlockFormat
public void setBlockFormat(String tag)"p", "h1" ..
"h6", "pre" and "blockquote".Parameters
tagString- the block tag name
setFontSize
public void setFontSize(int size)Parameters
sizeint- a value between 1 and 7
removeFormat
public void removeFormat()undo
public void undo()redo
public void redo()queryCommandState
public void queryCommandState(String command, SuccessCallback<Boolean> callback)"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
commandString- the command name to test
callbackSuccessCallback<Boolean>- receives true if the command is active for the current selection