Class RichTextArea
- All Implemented Interfaces:
Animation, Editable, 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).
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));
-
Field Summary
Fields 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.voidgetHtml(SuccessCallback<String> callback) Retrieves the current editor content as an HTML string.Returns the current placeholder text.voidgetText(SuccessCallback<String> callback) Retrieves the current editor content as plain text (markup stripped).voidindent()Increases the indentation of the current block.voidinsertHtml(String html) Inserts the supplied HTML fragment at the current cursor position.voidinsertImage(String url) Inserts an image at the current cursor position.voidConverts the current block(s) into an ordered (numbered) list.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.voidsetBlockFormat(String tag) Applies a block format / heading to the current block.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.voidsetPlaceholder(String text) Sets the placeholder text shown when the editor is empty.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, fireEditorEvent, focusEditor, getInternalBrowser, isEditable, isEditorReady, isNativeEditor, onReady, query, removeChangeListener, removeReadyListener, setEditableMethods 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
addDragFinishedListener, addDragOverListener, addDropListener, addFocusListener, addLongPressListener, addPointerDraggedListener, addPointerPressedListener, addPointerReleasedListener, addPullToRefresh, addScrollListener, addStateChangeListener, animate, announceForAccessibility, bindProperty, blocksSideSwipe, calcScrollSize, contains, containsOrOwns, createStyleAnimation, deinitialize, deinitializeCustomStyle, dragEnter, dragExit, dragFinished, draggingOver, drawDraggedImage, focusGained, focusLost, getAbsoluteX, getAbsoluteY, 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, 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, removeDragFinishedListener, removeDragOverListener, removeDropListener, removeFocusListener, removeLongPressListener, removePointerDraggedListener, removePointerPressedListener, removePointerReleasedListener, removeScrollListener, removeStateChangeListener, repaint, repaint, requestFocus, resetFocusable, respondsToPointerEvents, 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
-
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
-
insertHtml
Inserts the supplied HTML fragment at the current cursor position.
Parameters
html: the HTML fragment to insert
-
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
-
-