APIdia Launch Partner
Top Description Fields Constructors Methods
javax.swing

public Class JTextPane

extends JEditorPane
Class Inheritance
Annotations
@JavaBean
description:A text component that can be marked up with attributes that are graphically represented.
@SwingContainer
@SuppressWarnings:serial
Imports
java.awt.Component, java.beans.BeanProperty, .JavaBean, java.io.IOException, .ObjectOutputStream, .Serial, javax.swing.text.AbstractDocument, .AttributeSet, .BadLocationException, .Caret, .Document, .EditorKit, .Element, .MutableAttributeSet, .Style, .StyleConstants, .StyledDocument, .StyledEditorKit

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 java.beans.XMLEncoder.

Author
Timothy Prinzing
Since
1.2
See Also
javax.swing.text.StyledEditorKit

Field Summary

Modifier and TypeField and Description
private static final String
Inherited from javax.swing.JEditorPane:
defaultEditorKitMapHONOR_DISPLAY_PROPERTIESPostDataPropertyW3C_LENGTH_UNITS

Constructor Summary

AccessConstructor and Description
public
JTextPane()

Creates a new JTextPane.

public
JTextPane(StyledDocument
the document model
doc
)

Creates a new JTextPane, with a specified document model.

Method Summary

Modifier and TypeMethod and Description
public Style

Returns:

the new Style
addStyle
(String
the name of the style (must be unique within the collection of named styles). The name may be null if the style is unnamed, but the caller is responsible for managing the reference returned as an unnamed style can't be fetched by name. An unnamed style may be useful for things like character attribute overrides such as found in a style run.
nm
,
Style
the parent style. This may be null if unspecified attributes need not be resolved in some other style.
parent
)

Adds a new style into the logical style hierarchy.

protected EditorKit

Returns:

the editor kit
createDefaultEditorKit
()

Overrides javax.swing.JEditorPane.createDefaultEditorKit.

Creates the EditorKit to use by default.

public AttributeSet

Returns:

the attributes, or null
getCharacterAttributes
()

Fetches the character attributes in effect at the current location of the caret, or null.

public MutableAttributeSet

Returns:

the attributes
getInputAttributes
()

Gets the input attributes for the pane.

public Style

Returns:

the Style
getLogicalStyle
()

Fetches the logical style assigned to the paragraph represented by the current position of the caret, or null.

public AttributeSet

Returns:

the attributes
getParagraphAttributes
()

Fetches the current paragraph attributes in effect at the location of the caret, or null if none.

public Style

Returns:

the Style
getStyle
(String
the name of the style
nm
)

Fetches a named non-null style previously added.

public StyledDocument

Returns:

the model
getStyledDocument
()

Fetches the model associated with the editor.

protected final StyledEditorKit

Returns:

the editor kit
getStyledEditorKit
()

Gets the editor kit.

public String

Returns:

the string "TextPaneUI"
getUIClassID
()

Overrides javax.swing.JEditorPane.getUIClassID.

Returns the class ID for the UI.

public void
insertComponent(Component
the component to insert
c
)

Inserts a component into the document as a replacement for the currently selected content.

public void
insertIcon(Icon
the icon to insert
g
)

Inserts an icon into the document as a replacement for the currently selected content.

protected String

Returns:

a string representation of this JTextPane
paramString
()

Overrides javax.swing.JEditorPane.paramString.

Returns a string representation of this JTextPane.

public void
removeStyle(String
the name of the style to remove
nm
)

Removes a named non-null style previously added to the document.

public void
replaceSelection(String
the content to replace the selection with
content
)

Overrides javax.swing.JEditorPane.replaceSelection.

Replaces the currently selected content with new content represented by the given string.

private void
replaceSelection(String content, boolean checkEditable)

public void
setCharacterAttributes(AttributeSet
the attributes
attr
,
boolean
if true, then replace the existing attributes first
replace
)

Applies the given attributes to character content.

public void
setDocument(Document
the document to display/edit
doc
)

Overrides javax.swing.text.JTextComponent.setDocument.

Associates the editor with a text document.

public final void
setEditorKit(EditorKit
the desired editor behavior
kit
)

Overrides javax.swing.JEditorPane.setEditorKit.

Sets the currently installed kit for handling content.

public void
setLogicalStyle(Style
the logical style to assign to the paragraph, or null for no style
s
)

Sets the logical style to use for the paragraph at the current caret position.

public void
setParagraphAttributes(AttributeSet
the non-null attributes
attr
,
boolean
if true, replace the existing attributes first
replace
)

Applies the given attributes to paragraphs.

public void
setStyledDocument(StyledDocument
the document to display/edit
doc
)

Associates the editor with a text document.

private void
writeObject(ObjectOutputStream
the output stream
s
)

Hides javax.swing.JEditorPane.writeObject.

See readObject and writeObject in JComponent for more information about serialization in Swing.

Inherited from javax.swing.JEditorPane:
addHyperlinkListenercreateEditorKitForContentTypefireHyperlinkUpdategetAccessibleContextgetContentTypegetEditorKitgetEditorKitClassNameForContentTypegetEditorKitForContentTypegetHyperlinkListenersgetPagegetPreferredSizegetScrollableTracksViewportHeightgetScrollableTracksViewportWidthgetStreamgetTextreadreadregisterEditorKitForContentTyperegisterEditorKitForContentTyperemoveHyperlinkListenerscrollToReferencesetContentTypesetEditorKitForContentTypesetPagesetPagesetText

Field Detail

uiClassIDback to summary
private static final String uiClassID

Hides javax.swing.JEditorPane.uiClassID.

See Also
getUIClassID, readObject

Constructor Detail

JTextPaneback to summary
public JTextPane()

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

JTextPaneback to summary
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:StyledDocument

the document model

Method Detail

addStyleback to summary
public Style addStyle(String nm, Style parent)

Adds a new style into the logical style hierarchy. Style attributes resolve from bottom up so an attribute specified in a child will override an attribute specified in the parent.

Parameters
nm:String

the name of the style (must be unique within the collection of named styles). The name may be null if the style is unnamed, but the caller is responsible for managing the reference returned as an unnamed style can't be fetched by name. An unnamed style may be useful for things like character attribute overrides such as found in a style run.

parent:Style

the parent style. This may be null if unspecified attributes need not be resolved in some other style.

Returns:Style

the new Style

createDefaultEditorKitback to summary
protected EditorKit createDefaultEditorKit()

Overrides javax.swing.JEditorPane.createDefaultEditorKit.

Creates the EditorKit to use by default. This is implemented to return javax.swing.text.StyledEditorKit.

Returns:EditorKit

the editor kit

getCharacterAttributesback to summary
public AttributeSet getCharacterAttributes()

Fetches the character attributes in effect at the current location of the caret, or null.

Returns:AttributeSet

the attributes, or null

Annotations
@BeanProperty
bound:false
getInputAttributesback to summary
public MutableAttributeSet getInputAttributes()

Gets the input attributes for the pane.

Returns:MutableAttributeSet

the attributes

Annotations
@BeanProperty
bound:false
getLogicalStyleback to summary
public Style getLogicalStyle()

Fetches the logical style assigned to the paragraph represented by the current position of the caret, or null.

Returns:Style

the Style

getParagraphAttributesback to summary
public AttributeSet getParagraphAttributes()

Fetches the current paragraph attributes in effect at the location of the caret, or null if none.

Returns:AttributeSet

the attributes

Annotations
@BeanProperty
bound:false
getStyleback to summary
public Style getStyle(String nm)

Fetches a named non-null style previously added.

Parameters
nm:String

the name of the style

Returns:Style

the Style

getStyledDocumentback to summary
public StyledDocument getStyledDocument()

Fetches the model associated with the editor.

Returns:StyledDocument

the model

getStyledEditorKitback to summary
protected final StyledEditorKit getStyledEditorKit()

Gets the editor kit.

Returns:StyledEditorKit

the editor kit

getUIClassIDback to summary
public String getUIClassID()

Overrides javax.swing.JEditorPane.getUIClassID.

Returns the class ID for the UI.

Returns:String

the string "TextPaneUI"

Annotations
@BeanProperty
bound:false
See Also
JComponent#getUIClassID, UIDefaults#getUI
insertComponentback to summary
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:Component

the component to insert

insertIconback to summary
public void insertIcon(Icon g)

Inserts an icon into the document as a replacement for the currently selected content. If there is no selection the icon 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.

Parameters
g:Icon

the icon to insert

See Also
Icon
paramStringback to summary
protected String paramString()

Overrides javax.swing.JEditorPane.paramString.

Returns a string representation of this JTextPane. This method is intended to be used only for debugging purposes, and the content and format of the returned string may vary between implementations. The returned string may be empty but may not be null.

Returns:String

a string representation of this JTextPane

removeStyleback to summary
public void removeStyle(String nm)

Removes a named non-null style previously added to the document.

Parameters
nm:String

the name of the style to remove

replaceSelectionback to summary
public void replaceSelection(String content)

Overrides javax.swing.JEditorPane.replaceSelection.

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.

Parameters
content:String

the content to replace the selection with

Annotations
@Override
replaceSelectionback to summary
private void replaceSelection(String content, boolean checkEditable)
setCharacterAttributesback to summary
public void setCharacterAttributes(AttributeSet attr, boolean replace)

Applies the given attributes to character content. If there is a selection, the attributes are applied to the selection range. If there is no selection, the attributes are applied to the input attribute set which defines the attributes for any new text that gets inserted.

Parameters
attr:AttributeSet

the attributes

replace:boolean

if true, then replace the existing attributes first

setDocumentback to summary
public void setDocument(Document doc)

Overrides javax.swing.text.JTextComponent.setDocument.

Associates the editor with a text document. This must be a StyledDocument.

Parameters
doc:Document

the document to display/edit

Exceptions
IllegalArgumentException:
if doc can't be narrowed to a StyledDocument which is the required type of model for this text component
setEditorKitback to summary
public final void setEditorKit(EditorKit kit)

Overrides javax.swing.JEditorPane.setEditorKit.

Sets the currently installed kit for handling content. This is the bound property that establishes the content type of the editor.

Parameters
kit:EditorKit

the desired editor behavior

Exceptions
IllegalArgumentException:
if kit is not a StyledEditorKit
setLogicalStyleback to summary
public void setLogicalStyle(Style s)

Sets the logical style to use for the paragraph at the current caret position. If attributes aren't explicitly set for character and paragraph attributes they will resolve through the logical style assigned to the paragraph, which in term may resolve through some hierarchy completely independent of the element hierarchy in the document.

Parameters
s:Style

the logical style to assign to the paragraph, or null for no style

setParagraphAttributesback to summary
public void setParagraphAttributes(AttributeSet attr, boolean replace)

Applies the given attributes to paragraphs. If there is a selection, the attributes are applied to the paragraphs that intersect the selection. If there is no selection, the attributes are applied to the paragraph at the current caret position.

Parameters
attr:AttributeSet

the non-null attributes

replace:boolean

if true, replace the existing attributes first

setStyledDocumentback to summary
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:StyledDocument

the document to display/edit

writeObjectback to summary
private void writeObject(ObjectOutputStream s) throws IOException

Hides javax.swing.JEditorPane.writeObject.

See readObject and writeObject in JComponent for more information about serialization in Swing.

Parameters
s:ObjectOutputStream

the output stream

Annotations
@Serial
Exceptions
IOException:

Doc from java.awt.Container.writeObject.

if an I/O error occurs