javax.swing

Class JTextPane

public class JTextPane extends JEditorPane

A powerful text component that supports styled content as well as embedding images and components. It is entirely based on a {@link StyledDocument} content model and a {@link StyledEditorKit}.
Constructor Summary
JTextPane()
Creates a new JTextPane with a null document.
JTextPane(StyledDocument document)
Creates a new JTextPane and sets the specified document.
Method Summary
StyleaddStyle(String nm, Style parent)
Adds a style into the style hierarchy.
protected EditorKitcreateDefaultEditorKit()
Creates the default {@link EditorKit} that is used in JTextPanes.
AttributeSetgetCharacterAttributes()
Returns the text attributes for the character at the current caret position.
MutableAttributeSetgetInputAttributes()
Returns the attributes that are applied to newly inserted text.
StylegetLogicalStyle()
Returns the logical style of the paragraph at the current caret position.
AttributeSetgetParagraphAttributes()
Returns the text attributes of the paragraph at the current caret position.
StylegetStyle(String nm)
Looks up and returns a named Style.
StyledDocumentgetStyledDocument()
Returns the {@link StyledDocument} that is the content model for this JTextPane.
protected StyledEditorKitgetStyledEditorKit()
Returns the {@link StyledEditorKit} that is currently used by this JTextPane.
StringgetUIClassID()
Returns the UI class ID.
voidinsertComponent(Component component)
Inserts an AWT or Swing component into the text at the current caret position.
voidinsertIcon(Icon icon)
Inserts an Icon into the text at the current caret position.
protected StringparamString()
Returns a param string that can be used for debugging.
voidremoveStyle(String nm)
Removes a named Style from the style hierarchy.
voidreplaceSelection(String content)
Replaces the currently selected text with the specified content.
voidsetCharacterAttributes(AttributeSet attribute, boolean replace)
Sets text attributes for the current selection.
voidsetDocument(Document document)
Sets the content model for this JTextPane.
voidsetEditorKit(EditorKit editor)
Sets the {@link EditorKit} to use for this JTextPane.
voidsetLogicalStyle(Style style)
Sets the logical style for the paragraph at the current caret position.
voidsetParagraphAttributes(AttributeSet attribute, boolean replace)
Sets text attributes for the paragraph at the current selection.
voidsetStyledDocument(StyledDocument document)
Sets the content model for this JTextPane.

Constructor Detail

JTextPane

public JTextPane()
Creates a new JTextPane with a null document.

JTextPane

public JTextPane(StyledDocument document)
Creates a new JTextPane and sets the specified document.

Parameters: document the content model to use

Method Detail

addStyle

public Style addStyle(String nm, Style parent)
Adds a style into the style hierarchy. Unspecified style attributes can be resolved in the parent style, if one is specified. While it is legal to add nameless styles (nm == nullParameters: nm the name of the style or null if the style should be unnamed parent the parent in which unspecified style attributes are resolved, or null if that is not necessary

Returns: the newly created Style

createDefaultEditorKit

protected EditorKit createDefaultEditorKit()
Creates the default {@link EditorKit} that is used in JTextPanes. This is an instance of {@link StyledEditorKit}.

Returns: the default {@link EditorKit} that is used in JTextPanes

getCharacterAttributes

public AttributeSet getCharacterAttributes()
Returns the text attributes for the character at the current caret position.

Returns: the text attributes for the character at the current caret position

getInputAttributes

public MutableAttributeSet getInputAttributes()
Returns the attributes that are applied to newly inserted text. This is a {@link MutableAttributeSet}, so you can easily modify these attributes.

Returns: the attributes that are applied to newly inserted text

getLogicalStyle

public Style getLogicalStyle()
Returns the logical style of the paragraph at the current caret position.

Returns: the logical style of the paragraph at the current caret position

getParagraphAttributes

public AttributeSet getParagraphAttributes()
Returns the text attributes of the paragraph at the current caret position.

Returns: the attributes of the paragraph at the current caret position

getStyle

public Style getStyle(String nm)
Looks up and returns a named Style.

Parameters: nm the name of the Style

Returns: the found Style of null if no such Style exists

getStyledDocument

public StyledDocument getStyledDocument()
Returns the {@link StyledDocument} that is the content model for this JTextPane. This is a typed wrapper for {@link #getDocument()}.

Returns: the content model of this JTextPane

getStyledEditorKit

protected final StyledEditorKit getStyledEditorKit()
Returns the {@link StyledEditorKit} that is currently used by this JTextPane.

Returns: the current StyledEditorKit of this JTextPane

getUIClassID

public String getUIClassID()
Returns the UI class ID. This is TextPaneUI.

Returns: TextPaneUI

insertComponent

public void insertComponent(Component component)
Inserts an AWT or Swing component into the text at the current caret position.

Parameters: component the component to be inserted

insertIcon

public void insertIcon(Icon icon)
Inserts an Icon into the text at the current caret position.

Parameters: icon the Icon to be inserted

paramString

protected String paramString()
Returns a param string that can be used for debugging.

Returns: a param string that can be used for debugging.

removeStyle

public void removeStyle(String nm)
Removes a named Style from the style hierarchy.

Parameters: nm the name of the Style to be removed

replaceSelection

public void replaceSelection(String content)
Replaces the currently selected text with the specified content. If there is no selected text, this results in a simple insertion at the current caret position. If there is no content specified, this results in the selection beeing deleted.

Parameters: content the text with which the selection is replaced

setCharacterAttributes

public void setCharacterAttributes(AttributeSet attribute, boolean replace)
Sets text attributes for the current selection. If there is no selection the text attributes are applied to newly inserted text

Parameters: attribute the text attributes to set replace if true, the attributes of the current selection are overridden, otherwise they are merged

See Also: JTextPane

setDocument

public void setDocument(Document document)
Sets the content model for this JTextPane. JTextPane can only be used with {@link StyledDocument}s, if you try to set a different type of Document, an IllegalArgumentException is thrown.

Parameters: document the content model to set

Throws: IllegalArgumentException if document is not an instance of StyledDocument

See Also: JTextPane

setEditorKit

public final void setEditorKit(EditorKit editor)
Sets the {@link EditorKit} to use for this JTextPane. JTextPanes can only handle {@link StyledEditorKit}s, if client programs try to set a different type of EditorKit then an IllegalArgumentException is thrown

Parameters: editor the EditorKit to set

Throws: IllegalArgumentException if editor is no StyledEditorKit

setLogicalStyle

public void setLogicalStyle(Style style)
Sets the logical style for the paragraph at the current caret position.

Parameters: style the style to set for the current paragraph

setParagraphAttributes

public void setParagraphAttributes(AttributeSet attribute, boolean replace)
Sets text attributes for the paragraph at the current selection. If there is no selection the text attributes are applied to the paragraph at the current caret position.

Parameters: attribute the text attributes to set replace if true, the attributes of the current selection are overridden, otherwise they are merged

setStyledDocument

public void setStyledDocument(StyledDocument document)
Sets the content model for this JTextPane.

Parameters: document the content model to set