Class JTextPane

All Implemented Interfaces:
ImageObserver, MenuContainer, Serializable, Accessible, Scrollable

public class JTextPane extends JEditorPane
Emulator for JTextPane — a thin subclass of JEditorPane over the same SJEditorPane (RTE) peer, backed by a DefaultStyledDocument whose styled runs render through RTE's HTML value (D_jtextpane).
JTextPane pane = new JTextPane();
StyledDocument doc = pane.getStyledDocument();
SimpleAttributeSet red = new SimpleAttributeSet();
StyleConstants.setForeground(red, Color.RED);
StyleConstants.setBold(red, true);
doc.insertString(0, "alert", red);   // renders bold red in the browser

What renders (the RTE subset)

Bold, italic, underline, strike-through, foreground/background colour, super/subscript, paragraph alignment, and ImageIcon embedding all map to HTML and render. Anything RTE can't represent (font family/size, indents, line spacing, insertComponent, a non-ImageIcon icon) WARNs per call and is dropped — see RteHtmlCodec.

getText() is plain text

Unlike JEditorPane (whose getText() is HTML), a JTextPane's document holds the visible text with styling carried on run attributes, so getText() returns plain text — matching real JTextPane. A consequence: setText("<b>hi</b>") shows the literal string, as a plain-StyledEditorKit JTextPane would, not bold.

Two-way sync

A DocumentListener serializes the document → RTE-subset HTML and pushes it through the peer's main value on every mutation (so doc.insertString/setCharacterAttributes render). Browser edits fire the peer's value-change; the reverse reads that same HTML value back (getValue()) and rebuilds the document (clear + re-insert) so getText()/getStyledDocument() reflect them and user DocumentListeners fire (R_swing_is_truth/R_callswing_envelope). preventPeerEvents guards the loop. One value format both ways: reading getValue() — the property the change event fired on — avoids the cross-property staleness of reading asDelta() (a different property that can lag the htmlValue edit), and push stays HTML because asDelta().setValue's async htmlValue reconcile races Quill to empty during construction — see RteHtmlCodec.toHtml(StyledDocument).

Selection-based styled methods

The selection-based setCharacterAttributes(AttributeSet, boolean)/getCharacterAttributes() operate on the pane's input attributes whatever the caret selects (the JDK's no-selection behaviour); programmatic styling that actually renders goes through the offset-based getStyledDocument().setCharacterAttributes(offset, len, …) path.

R_leaf_peer_lockdown: leaf lock-down

No public javax.swing.* class extends JTextPane, so it is a leaf: it omits the protected (Component peer) ctor and hard-codes new SJEditorPane() through JEditorPane's protected D_peer_ctor_injection seam. Non-final so behaviour-subclassing still works.
See Also:
  • Constructor Details

  • Method Details

    • createDefaultDocument

      protected Document createDefaultDocument()
      Description copied from class: JTextComponent
      Factory for the default Document. Real Swing subclasses override to pick richer Document models (the EditorKit-driven path in JEditorPane, etc.). Our base picks PlainDocument — the simplest line- oriented model, and what JTextField would pick anyway.
      Overrides:
      createDefaultDocument in class JEditorPane
    • getStyledDocument

      public StyledDocument getStyledDocument()
    • setStyledDocument

      public void setStyledDocument(StyledDocument doc)
      Convenience for setDocument(Document) typed to StyledDocument.
    • setDocument

      public void setDocument(Document doc)
      Overrides:
      setDocument in class JTextComponent
    • wirePeerToDocument

      protected void wirePeerToDocument(com.vaadin.swingbridge.surrogates.SJEditorPane editor)
      Description copied from class: JEditorPane
      Subscribes to browser edits and mirrors them into the Document (EAGER per R_swing_is_truth's "tracks typing", callSwing per R_callswing_envelope). Overridable seam: JTextPane drives its two-way sync through RTE's Delta value instead of HTML, so it replaces this wiring rather than adding a second, conflicting listener.
      Overrides:
      wirePeerToDocument in class JEditorPane
    • pushTextToPeer

      protected void pushTextToPeer(String t)
      Description copied from class: JEditorPane
      Pushes setText's markup into RTE's htmlValue under preventPeerEvents (so the peer's value-change listener doesn't re-mirror our own write back into the Document). Overridable seam: JTextPane pushes through its Delta serializer instead, driven by the Document mutation super.setText already made, so it overrides this to a no-op.
      Overrides:
      pushTextToPeer in class JEditorPane
    • setCharacterAttributes

      public void setCharacterAttributes(AttributeSet attr, boolean replace)
      Applies attr to the pane's input attributes whatever is selected, which makes no visible change: the JDK's no-selection behaviour, where the JDK would style a selection. To style existing text call getStyledDocument().setCharacterAttributes(...).
    • getCharacterAttributes

      public AttributeSet getCharacterAttributes()
      The pending-input character attributes (JDK returns these at an empty selection).
    • setParagraphAttributes

      public void setParagraphAttributes(AttributeSet attr, boolean replace)
      Applies paragraph attr to the first paragraph — the JDK's behaviour at an empty selection (setParagraphAttributes(0, 0, …)), which does render (e.g. alignment). Offset-based styling of other paragraphs goes through getStyledDocument().setParagraphAttributes(offset, len, …).
    • getParagraphAttributes

      public AttributeSet getParagraphAttributes()
      Paragraph attributes of the first paragraph (JDK reads these at the caret).
    • getInputAttributes

      public MutableAttributeSet getInputAttributes()
      Returns:
      the kit's pending-input attribute set — the JDK's own home for it, so a createDefaultEditorKit() override that seeds attributes is observable here
    • addStyle

      public Style addStyle(String nm, Style parent)
    • removeStyle

      public void removeStyle(String nm)
    • getStyle

      public Style getStyle(String nm)
    • setLogicalStyle

      public void setLogicalStyle(Style s)
      Sets the logical style of the first paragraph (JDK applies it at the caret).
    • getLogicalStyle

      public Style getLogicalStyle()
      Logical style of the first paragraph.
    • insertIcon

      public void insertIcon(Icon g)
      Embeds an image: an ImageIcon rasterises to a base64 PNG Delta image op and renders as <img>. Any other Icon WARNs and is dropped — rasterising a custom paintIcon needs server-side Graphics paint, which is OOS (R_match_swing_errors(b)).
    • insertComponent

      public void insertComponent(Component c)
      Drop-and-WARN: a live inline component has no Delta/RTE representation. Takes the ported Component (the import-swap type).
    • setEditorKit

      public final void setEditorKit(EditorKit kit)
      Description copied from class: JEditorPane
      Install an editor kit. Every kit is stored and read back by JEditorPane.getEditorKit(); only a HTMLEditorKit additionally takes effect, its stylesheet binding to the peer so rules added to it (before or after this call) style the pane's content. Any other kind WARNs — without a route to the Vaadin peer, installing it could only pretend.

      Two JDK behaviours are deliberately not reproduced. The document is left alone — the JDK replaces it with kit.createDefaultDocument(), whereas here the element model follows the content type instead (JEditorPane.setContentType(String)), so installing a kit never silently changes what getDocument() returns. And the content type is left alone; set it explicitly if it matters.

      Overrides:
      setEditorKit in class JEditorPane
    • createDefaultEditorKit

      protected EditorKit createDefaultEditorKit()
      Returns:
      the kit this pane holds; called from the constructor as the JDK calls it, so an override cannot read its own subclass's fields
    • getStyledEditorKit

      protected final StyledEditorKit getStyledEditorKit()
      Returns:
      the kit createDefaultEditorKit() built at construction — where the JDK casts getEditorKit(), which here answers only what the migrator installed (see JEditorPane.getEditorKit())
    • getUIClassID

      public String getUIClassID()
      Overrides:
      getUIClassID in class JEditorPane