JavaTM 2 Platform
Standard Edition

javax.swing
Class JTextPane

java.lang.Object
  |
  +--java.awt.Component
        |
        +--java.awt.Container
              |
              +--javax.swing.JComponent
                    |
                    +--javax.swing.text.JTextComponent
                          |
                          +--javax.swing.JEditorPane
                                |
                                +--javax.swing.JTextPane

public class JTextPane
extends JEditorPane

A text component that can be marked up with attributes that are represented graphically. 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 no overriden by attributes set on the paragraph or character run. Components and images may be embedded in the flow of text.

For the keyboard keys used by this component in the standard Look and Feel (L&F) renditions, see the JTextPane key assignments.

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. A future release of Swing will provide support for long term persistence.

See Also:
text.StyledEditorKit, Serialized Form

Inner classes inherited from class javax.swing.JEditorPane
JEditorPane.AccessibleJEditorPane, JEditorPane.AccessibleJEditorPaneHTML, JEditorPane.JEditorPaneAccessibleHypertextSupport
 
Inner classes inherited from class javax.swing.text.JTextComponent
JTextComponent.AccessibleJTextComponent, JTextComponent.KeyBinding
 
Inner classes inherited from class javax.swing.JComponent
JComponent.AccessibleJComponent
 
Fields inherited from class javax.swing.text.JTextComponent
DEFAULT_KEYMAP, FOCUS_ACCELERATOR_KEY
 
Fields inherited from class javax.swing.JComponent
accessibleContext, listenerList, TOOL_TIP_TEXT_KEY, ui, UNDEFINED_CONDITION, WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, WHEN_FOCUSED, WHEN_IN_FOCUSED_WINDOW
 
Fields inherited from class java.awt.Component
BOTTOM_ALIGNMENT, CENTER_ALIGNMENT, LEFT_ALIGNMENT, RIGHT_ALIGNMENT, TOP_ALIGNMENT
 
Constructor Summary
JTextPane()
          Constructs a new JTextPane.
JTextPane(StyledDocument doc)
          Constructs a new JTextPane, with a specified document model.
 
Method Summary
 Style addStyle(String nm, Style parent)
          Adds a new style into the logical style hierarchy.
protected  EditorKit createDefaultEditorKit()
          Creates the EditorKit to use by default.
 AttributeSet getCharacterAttributes()
          Fetches the character attributes in effect at the current location of the caret, or null.
 MutableAttributeSet getInputAttributes()
          Gets the input attributes for the pane.
 Style getLogicalStyle()
          Fetches the logical style assigned to the paragraph represented by the current position of the caret, or null.
 AttributeSet getParagraphAttributes()
          Fetches the current paragraph attributes in effect at the location of the caret, or null if none.
 boolean getScrollableTracksViewportWidth()
          Returns true if a viewport should always force the width of this Scrollable to match the width of the viewport.
 Style getStyle(String nm)
          Fetches a named non-null style previously added.
 StyledDocument getStyledDocument()
          Fetches the model associated with the editor.
protected  StyledEditorKit getStyledEditorKit()
          Gets the editor kit.
 String getUIClassID()
          Returns the class ID for the UI.
 void insertComponent(Component c)
          Inserts a component into the document as a replacement for the currently selected content.
 void insertIcon(Icon g)
          Inserts an icon into the document as a replacement for the currently selected content.
protected  String paramString()
          Returns a string representation of this JTextPane.
 void removeStyle(String nm)
          Removes a named non-null style previously added to the document.
 void replaceSelection(String content)
          Replaces the currently selected content with new content represented by the given string.
 void setCharacterAttributes(AttributeSet attr, boolean replace)
          Applies the given attributes to character content.
 void setDocument(Document doc)
          Associates the editor with a text document.
 void setEditorKit(EditorKit kit)
          Sets the currently installed kit for handling content.
 void setLogicalStyle(Style s)
          Sets the logical style to use for the paragraph at the current caret position.
 void setParagraphAttributes(AttributeSet attr, boolean replace)
          Applies the given attributes to paragraphs.
 void setStyledDocument(StyledDocument doc)
          Associates the editor with a text document.
 
Methods inherited from class javax.swing.JEditorPane
addHyperlinkListener, createEditorKitForContentType, fireHyperlinkUpdate, getAccessibleContext, getContentType, getEditorKit, getEditorKitForContentType, getPage, getPreferredSize, getScrollableTracksViewportHeight, getStream, getText, isManagingFocus, processComponentKeyEvent, read, registerEditorKitForContentType, registerEditorKitForContentType, removeHyperlinkListener, scrollToReference, setContentType, setEditorKitForContentType, setPage, setPage, setText
 
Methods inherited from class javax.swing.text.JTextComponent
addCaretListener, addInputMethodListener, addKeymap, copy, cut, fireCaretUpdate, getActions, getCaret, getCaretColor, getCaretPosition, getDisabledTextColor, getDocument, getFocusAccelerator, getHighlighter, getInputMethodRequests, getKeymap, getKeymap, getMargin, getPreferredScrollableViewportSize, getScrollableBlockIncrement, getScrollableUnitIncrement, getSelectedText, getSelectedTextColor, getSelectionColor, getSelectionEnd, getSelectionStart, getText, getUI, isEditable, isFocusTraversable, isOpaque, loadKeymap, modelToView, moveCaretPosition, paste, processInputMethodEvent, read, removeCaretListener, removeKeymap, removeNotify, select, selectAll, setCaret, setCaretColor, setCaretPosition, setDisabledTextColor, setEditable, setEnabled, setFocusAccelerator, setHighlighter, setKeymap, setMargin, setOpaque, setSelectedTextColor, setSelectionColor, setSelectionEnd, setSelectionStart, setUI, updateUI, viewToModel, write
 
Methods inherited from class javax.swing.JComponent
addAncestorListener, addNotify, addPropertyChangeListener, addPropertyChangeListener, addVetoableChangeListener, computeVisibleRect, contains, createToolTip, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, fireVetoableChange, getActionForKeyStroke, getAlignmentX, getAlignmentY, getAutoscrolls, getBorder, getBounds, getClientProperty, getComponentGraphics, getConditionForKeyStroke, getDebugGraphicsOptions, getGraphics, getHeight, getInsets, getInsets, getLocation, getMaximumSize, getMinimumSize, getNextFocusableComponent, getRegisteredKeyStrokes, getRootPane, getSize, getToolTipLocation, getToolTipText, getToolTipText, getTopLevelAncestor, getVisibleRect, getWidth, getX, getY, grabFocus, hasFocus, isDoubleBuffered, isFocusCycleRoot, isLightweightComponent, isOptimizedDrawingEnabled, isPaintingTile, isRequestFocusEnabled, isValidateRoot, paint, paintBorder, paintChildren, paintComponent, paintImmediately, paintImmediately, processFocusEvent, processKeyEvent, processMouseMotionEvent, putClientProperty, registerKeyboardAction, registerKeyboardAction, removeAncestorListener, removePropertyChangeListener, removePropertyChangeListener, removeVetoableChangeListener, repaint, repaint, requestDefaultFocus, requestFocus, resetKeyboardActions, reshape, revalidate, scrollRectToVisible, setAlignmentX, setAlignmentY, setAutoscrolls, setBackground, setBorder, setDebugGraphicsOptions, setDoubleBuffered, setFont, setForeground, setMaximumSize, setMinimumSize, setNextFocusableComponent, setPreferredSize, setRequestFocusEnabled, setToolTipText, setUI, setVisible, unregisterKeyboardAction, update
 
Methods inherited from class java.awt.Container
add, add, add, add, add, addContainerListener, addImpl, countComponents, deliverEvent, doLayout, findComponentAt, findComponentAt, getComponent, getComponentAt, getComponentAt, getComponentCount, getComponents, getLayout, insets, invalidate, isAncestorOf, layout, list, list, locate, minimumSize, paintComponents, preferredSize, print, printComponents, processContainerEvent, processEvent, remove, remove, removeAll, removeContainerListener, setCursor, setLayout, validate, validateTree
 
Methods inherited from class java.awt.Component
action, add, addComponentListener, addFocusListener, addKeyListener, addMouseListener, addMouseMotionListener, bounds, checkImage, checkImage, coalesceEvents, contains, createImage, createImage, disable, disableEvents, dispatchEvent, enable, enable, enableEvents, enableInputMethods, getBackground, getBounds, getColorModel, getComponentOrientation, getCursor, getDropTarget, getFont, getFontMetrics, getForeground, getInputContext, getLocale, getLocation, getLocationOnScreen, getName, getParent, getPeer, getSize, getToolkit, getTreeLock, gotFocus, handleEvent, hide, imageUpdate, inside, isDisplayable, isEnabled, isLightweight, isShowing, isValid, isVisible, keyDown, keyUp, list, list, list, location, lostFocus, mouseDown, mouseDrag, mouseEnter, mouseExit, mouseMove, mouseUp, move, nextFocus, paintAll, postEvent, prepareImage, prepareImage, printAll, processComponentEvent, processMouseEvent, remove, removeComponentListener, removeFocusListener, removeInputMethodListener, removeKeyListener, removeMouseListener, removeMouseMotionListener, repaint, repaint, repaint, resize, resize, setBounds, setBounds, setComponentOrientation, setDropTarget, setLocale, setLocation, setLocation, setName, setSize, setSize, show, show, size, toString, transferFocus
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

JTextPane

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

JTextPane

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

getUIClassID

public String getUIClassID()
Returns the class ID for the UI.
Overrides:
getUIClassID in class JEditorPane
Returns:
the ID ("TextPaneUI")
See Also:
JComponent.getUIClassID(), UIDefaults.getUI(javax.swing.JComponent)

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

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. If the document is not editable, beep and return. Then if the document is null, do nothing. If the content to insert is null or empty, ignore it.

This method is thread safe, although most Swing methods are not. Please see Threads and Swing for more information.

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.
Parameters:
c - the component to insert

insertIcon

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.

This method is thread safe, although most Swing methods are not. Please see Threads and Swing for more information.

Parameters:
g - the icon to insert
See Also:
Icon

addStyle

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 - 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 - the parent style. This may be null if unspecified attributes need not be resolved in some other style.
Returns:
the new Style

removeStyle

public void removeStyle(String nm)
Removes a named non-null style previously added to the document.
Parameters:
nm - the name of the style to remove

getStyle

public Style getStyle(String nm)
Fetches a named non-null style previously added.
Parameters:
nm - the name of the style
Returns:
the style

setLogicalStyle

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 independant of the element hierarchy in the document.

This method is thread safe, although most Swing methods are not. Please see Threads and Swing for more information.

Parameters:
s - the logical style to assign to the paragraph, or null for no style

getLogicalStyle

public Style getLogicalStyle()
Fetches the logical style assigned to the paragraph represented by the current position of the caret, or null.
Returns:
the style

getCharacterAttributes

public AttributeSet getCharacterAttributes()
Fetches the character attributes in effect at the current location of the caret, or null.
Returns:
the attributes, or null

setCharacterAttributes

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.

This method is thread safe, although most Swing methods are not. Please see Threads and Swing for more information.

Parameters:
attr - the attributes
replace - if true, then replace the existing attributes first

getParagraphAttributes

public AttributeSet getParagraphAttributes()
Fetches the current paragraph attributes in effect at the location of the caret, or null if none.
Returns:
the attributes

setParagraphAttributes

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.

This method is thread safe, although most Swing methods are not. Please see Threads and Swing for more information.

Parameters:
attr - the non-null attributes
replace - if true, replace the existing attributes first

getInputAttributes

public MutableAttributeSet getInputAttributes()
Gets the input attributes for the pane.
Returns:
the attributes

getStyledEditorKit

protected final StyledEditorKit getStyledEditorKit()
Gets the editor kit.
Returns:
the editor kit.

createDefaultEditorKit

protected EditorKit createDefaultEditorKit()
Creates the EditorKit to use by default. This is implemented to return text.StyledEditorKit.
Overrides:
createDefaultEditorKit in class JEditorPane
Returns:
the editor kit

setEditorKit

public final void setEditorKit(EditorKit kit)
Sets the currently installed kit for handling content. This is the bound property that establishes the content type of the editor.
Overrides:
setEditorKit in class JEditorPane
Parameters:
kit - the desired editor behavior.
Throws:
IllegalArgumentException - if kit is not a text.StyledEditorKit

getScrollableTracksViewportWidth

public boolean getScrollableTracksViewportWidth()
Returns true if a viewport should always force the width of this Scrollable to match the width of the viewport.
Overrides:
getScrollableTracksViewportWidth in class JEditorPane
Returns:
true if a viewport should force the Scrollables width to match its own.

paramString

protected String 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.
Overrides:
paramString in class JEditorPane
Returns:
a string representation of this JTextPane.

JavaTM 2 Platform
Standard Edition

Submit a bug or feature
Java, Java 2D, and JDBC are a trademarks or registered trademarks of Sun Microsystems, Inc. in the US and other countries.
Copyright 1993-1999 Sun Microsystems, Inc. 901 San Antonio Road,
Palo Alto, California, 94303, U.S.A. All Rights Reserved.