Module java.desktop
Package javax.swing

Class JTextPane

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

@JavaBean(description="A text component that can be marked up with attributes that are graphically represented.") public class JTextPane extends JEditorPane
A text component that can be marked up with attributes that are represented graphically. You can find how-to information and examples of using text panes in Using Text Components, a section in The Java Tutorial.

This component models paragraphs that are composed of runs of character level attributes. Each paragraph may have a logical style attached to it which contains the default attributes to use if not overridden by attributes set on the paragraph or character run. Components and images may be embedded in the flow of text.

Newlines
For a discussion on how newlines are handled, see DefaultEditorKit.

Warning: Swing is not thread safe. For more information see Swing's Threading Policy.

Warning: Serialized objects of this class will not be compatible with future Swing releases. The current serialization support is appropriate for short term storage or RMI between applications running the same version of Swing. As of 1.4, support for long term storage of all JavaBeans has been added to the java.beans package. Please see XMLEncoder.

Since:
1.2
See Also:
  • Constructor Details

    • JTextPane

      public JTextPane()
      Creates a new JTextPane. A new instance of StyledEditorKit is created and set, and the document model set to null.
    • JTextPane

      public JTextPane(StyledDocument doc)
      Creates a new JTextPane, with a specified document model. A new instance of javax.swing.text.StyledEditorKit is created and set.
      Parameters:
      doc - the document model
  • Method Details

    • getUIClassID

      @BeanProperty(bound=false) public String getUIClassID()
      Returns the class ID for the UI.
      Overrides:
      getUIClassID in class JEditorPane
      Returns:
      the string "TextPaneUI"
      See Also:
    • setDocument

      public void setDocument(Document doc)
      Associates the editor with a text document. This must be a StyledDocument.
      Overrides:
      setDocument in class JTextComponent
      Parameters:
      doc - the document to display/edit
      Throws:
      IllegalArgumentException - if doc can't be narrowed to a StyledDocument which is the required type of model for this text component
      See Also:
    • setStyledDocument

      public void setStyledDocument(StyledDocument doc)
      Associates the editor with a text document. The currently registered factory is used to build a view for the document, which gets displayed by the editor.
      Parameters:
      doc - the document to display/edit
    • getStyledDocument

      public StyledDocument getStyledDocument()
      Fetches the model associated with the editor.
      Returns:
      the model
    • replaceSelection

      public void replaceSelection(String content)
      Replaces the currently selected content with new content represented by the given string. If there is no selection this amounts to an insert of the given text. If there is no replacement text this amounts to a removal of the current selection. The replacement text will have the attributes currently defined for input at the point of insertion. If the document is not editable, beep and return.
      Overrides:
      replaceSelection in class JEditorPane
      Parameters:
      content - the content to replace the selection with
    • insertComponent

      public void insertComponent(Component c)
      Inserts a component into the document as a replacement for the currently selected content. If there is no selection the component is effectively inserted at the current position of the caret. This is represented in the associated document as an attribute of one character of content.

      The component given is the actual component used by the JTextPane. Since components cannot be a child of more than one container, this method should not be used in situations where the model is shared by text components.

      The component is placed relative to the text baseline according to the value returned by Component.getAlignmentY. For Swing components this value can be conveniently set using the method JComponent.setAlignmentY. For example, setting a value of 0.75 will cause 75 percent of the component to be above the baseline, and 25 percent of the component to be below the baseline.

      Parameters:
      c - the component to insert
    • insertIcon

      public void insertIcon(