javax.swing
Class JTextPane

java.lang.Object
  extended by java.awt.Component
      extended by java.awt.Container
          extended by javax.swing.JComponent
              extended by javax.swing.text.JTextComponent
                  extended by javax.swing.JEditorPane
                      extended by javax.swing.JTextPane
All Implemented Interfaces:
ImageObserver, MenuContainer, Serializable, Accessible, Scrollable

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 StyledDocument content model and a StyledEditorKit.

See Also:
Serialized Form

Nested Class Summary
 
Nested classes/interfaces inherited from class javax.swing.JEditorPane
JEditorPane.AccessibleJEditorPane, JEditorPane.AccessibleJEditorPaneHTML, JEditorPane.JEditorPaneAccessibleHypertextSupport
 
Nested classes/interfaces inherited from class javax.swing.text.JTextComponent
JTextComponent.AccessibleJTextComponent, JTextComponent.KeyBinding
 
Nested classes/interfaces inherited from class javax.swing.JComponent
JComponent.AccessibleJComponent
 
Nested classes/interfaces inherited from class java.awt.Container
Container.AccessibleAWTContainer
 
Nested classes/interfaces inherited from class java.awt.Component
Component.AccessibleAWTComponent, Component.BltBufferStrategy, Component.FlipBufferStrategy
 
Field Summary
 
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
 
Fields inherited from interface java.awt.image.ImageObserver
ABORT, ALLBITS, ERROR, FRAMEBITS, HEIGHT, PROPERTIES, SOMEBITS, WIDTH
 
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
 Style addStyle(String nm, Style parent)
          Adds a style into the style hierarchy.
protected  EditorKit createDefaultEditorKit()
          Creates the default EditorKit that is used in JTextPanes.
 AttributeSet getCharacterAttributes()
          Returns the text attributes for the character at the current caret position.
 MutableAttributeSet getInputAttributes()
          Returns the attributes that are applied to newly inserted text.
 Style getLogicalStyle()
          Returns the logical style of the paragraph at the current caret position.
 AttributeSet getParagraphAttributes()
          Returns the text attributes of the paragraph at the current caret position.
 Style getStyle(String nm)
          Looks up and returns a named Style.
 StyledDocument getStyledDocument()
          Returns the StyledDocument that is the content model for this JTextPane.
protected  StyledEditorKit getStyledEditorKit()
          Returns the StyledEditorKit that is currently used by this JTextPane.
 String getUIClassID()
          Returns the UI class ID.
 void insertComponent(Component component)
          Inserts an AWT or Swing component into the text at the current caret position.
 void insertIcon(Icon icon)
          Inserts an Icon into the text at the current caret position.
protected  String paramString()
          Returns a param string that can be used for debugging.
 void removeStyle(String nm)
          Removes a named Style from the style hierarchy.
 void replaceSelection(String content)
          Replaces the currently selected text with the specified content.
 void setCharacterAttributes(AttributeSet attribute, boolean replace)
          Sets text attributes for the current selection.
 void setDocument(Document document)
          Sets the content model for this JTextPane.
 void setEditorKit(EditorKit editor)
          Sets the EditorKit to use for this JTextPane.
 void setLogicalStyle(Style style)
          Sets the logical style for the paragraph at the current caret position.
 void setParagraphAttributes(AttributeSet attribute, boolean replace)
          Sets text attributes for the paragraph at the current selection.
 void setStyledDocument(StyledDocument document)
          Sets the content model for this JTextPane.
 
Methods inherited from class javax.swing.JEditorPane
addHyperlinkListener, createEditorKitForContentType, fireHyperlinkUpdate, getAccessibleContext, getContentType, getEditorKit, getEditorKitClassNameForContentType, getEditorKitForContentType, getHyperlinkListeners, getPage, getPreferredSize, getScrollableTracksViewportHeight, getScrollableTracksViewportWidth, getStream, getText, isFocusCycleRoot, 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, getCaretListeners, getCaretPosition, getDisabledTextColor, getDocument, getDragEnabled, getFocusAccelerator, getHighlighter, getInputMethodListeners, getKeymap, getKeymap, getMargin, getNavigationFilter, getPreferredScrollableViewportSize, getScrollableBlockIncrement, getScrollableUnitIncrement, getSelectedText, getSelectedTextColor, getSelectionColor, getSelectionEnd, getSelectionStart, getText, getToolTipText, getUI, isEditable, loadKeymap, modelToView, moveCaretPosition, paste, read, removeCaretListener, removeInputMethodListener, removeKeymap, select, selectAll, setCaret, setCaretColor, setCaretPosition, setDisabledTextColor, setDragEnabled, setEditable, setFocusAccelerator, setHighlighter, setKeymap, setMargin, setNavigationFilter, setSelectedTextColor, setSelectionColor, setSelectionEnd, setSelectionStart, setUI, updateUI, viewToModel, write
 
Methods inherited from class javax.swing.JComponent
addAncestorListener, addNotify, addVetoableChangeListener, computeVisibleRect, contains, createToolTip, disable, enable, firePropertyChange, firePropertyChange, fireVetoableChange, getActionForKeyStroke, getActionMap, getAlignmentX, getAlignmentY, getAncestorListeners, getAutoscrolls, getBorder, getBounds, getClientProperty, getComponentGraphics, getComponentPopupMenu, getConditionForKeyStroke, getDebugGraphicsOptions, getDefaultLocale, getGraphics, getHeight, getInheritsPopupMenu, getInputMap, getInputMap, getInputVerifier, getInsets, getInsets, getListeners, getLocation, getMaximumSize, getMinimumSize, getNextFocusableComponent, getRegisteredKeyStrokes, getRootPane, getSize, getToolTipLocation, getToolTipText, getTopLevelAncestor, getTransferHandler, getVerifyInputWhenFocusTarget, getVetoableChangeListeners, getVisibleRect, getWidth, getX, getY, grabFocus, isDoubleBuffered, isLightweightComponent, isManagingFocus, isOpaque, isOptimizedDrawingEnabled, isPaintingTile, isRequestFocusEnabled, isValidateRoot, paint, paintBorder, paintChildren, paintComponent, paintImmediately, paintImmediately, print, printAll, printBorder, printChildren, printComponent, processComponentKeyEvent, processKeyBinding, processKeyEvent, processMouseMotionEvent, putClientProperty, registerKeyboardAction, registerKeyboardAction, removeAncestorListener, removeNotify, removeVetoableChangeListener, repaint, repaint, requestDefaultFocus, requestFocus, requestFocus, requestFocusInWindow, requestFocusInWindow, resetKeyboardActions, reshape, revalidate, scrollRectToVisible, setActionMap, setAlignmentX, setAlignmentY, setAutoscrolls, setBackground, setBorder, setComponentPopupMenu, setDebugGraphicsOptions, setDefaultLocale, setDoubleBuffered, setEnabled, setFont, setForeground, setInheritsPopupMenu, setInputMap, setInputVerifier, setNextFocusableComponent, setOpaque, setRequestFocusEnabled, setToolTipText, setTransferHandler, setUI, setVerifyInputWhenFocusTarget, setVisible, unregisterKeyboardAction, update
 
Methods inherited from class java.awt.Container
add, add, add, add, add, addContainerListener, addImpl, addPropertyChangeListener, addPropertyChangeListener, applyComponentOrientation, areFocusTraversalKeysSet, countComponents, deliverEvent, doLayout, findComponentAt, findComponentAt, getComponent, getComponentAt, getComponentAt, getComponentCount, getComponents, getComponentZOrder, getContainerListeners, getFocusTraversalKeys, getFocusTraversalPolicy, getLayout, getMousePosition, insets, invalidate, isAncestorOf, isFocusCycleRoot, isFocusTraversalPolicyProvider, isFocusTraversalPolicySet, layout, list, list, locate, minimumSize, paintComponents, preferredSize, printComponents, processContainerEvent, processEvent, remove, remove, removeAll, removeContainerListener, setComponentZOrder, setFocusCycleRoot, setFocusTraversalKeys, setFocusTraversalPolicy, setFocusTraversalPolicyProvider, setLayout, transferFocusDownCycle, validate, validateTree
 
Methods inherited from class java.awt.Component
action, add, addComponentListener, addFocusListener, addHierarchyBoundsListener, addHierarchyListener, addKeyListener, addMouseListener, addMouseMotionListener, addMouseWheelListener, bounds, checkImage, checkImage, coalesceEvents, contains, createImage, createImage, createVolatileImage, createVolatileImage, disableEvents, dispatchEvent, enable, enableEvents, enableInputMethods, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, getBackground, getBounds, getColorModel, getComponentListeners, getComponentOrientation, getCursor, getDropTarget, getFocusCycleRootAncestor, getFocusListeners, getFocusTraversalKeysEnabled, getFont, getFontMetrics, getForeground, getGraphicsConfiguration, getHierarchyBoundsListeners, getHierarchyListeners, getIgnoreRepaint, getInputContext, getInputMethodRequests, getKeyListeners, getLocale, getLocation, getLocationOnScreen, getMouseListeners, getMouseMotionListeners, getMousePosition, getMouseWheelListeners, getName, getParent, getPeer, getPropertyChangeListeners, getPropertyChangeListeners, getSize, getToolkit, getTreeLock, gotFocus, handleEvent, hasFocus, hide, imageUpdate, inside, isBackgroundSet, isCursorSet, isDisplayable, isEnabled, isFocusable, isFocusOwner, isFocusTraversable, isFontSet, isForegroundSet, isLightweight, isMaximumSizeSet, isMinimumSizeSet, isPreferredSizeSet, isShowing, isValid, isVisible, keyDown, keyUp, list, list, list, location, lostFocus, mouseDown, mouseDrag, mouseEnter, mouseExit, mouseMove, mouseUp, move, nextFocus, paintAll, postEvent, prepareImage, prepareImage, processComponentEvent, processFocusEvent, processHierarchyBoundsEvent, processHierarchyEvent, processInputMethodEvent, processMouseEvent, processMouseWheelEvent, remove, removeComponentListener, removeFocusListener, removeHierarchyBoundsListener, removeHierarchyListener, removeKeyListener, removeMouseListener, removeMouseMotionListener, removeMouseWheelListener, removePropertyChangeListener, removePropertyChangeListener, repaint, repaint, repaint, resize, resize, setBounds, setBounds, setComponentOrientation, setCursor, setDropTarget, setFocusable, setFocusTraversalKeysEnabled, setIgnoreRepaint, setLocale, setLocation, setLocation, setMaximumSize, setMinimumSize, setName, setPreferredSize, setSize, setSize, show, show, size, toString, transferFocus, transferFocusBackward, transferFocusUpCycle
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

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

getUIClassID

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

Overrides:
getUIClassID in class JEditorPane
Returns:
TextPaneUI
See Also:
JComponent.setUI(javax.swing.plaf.ComponentUI), JComponent.updateUI()

setDocument

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

Overrides:
setDocument in class JTextComponent
Parameters:
document - the content model to set
Throws:
IllegalArgumentException - if document is not an instance of StyledDocument
See Also:
setStyledDocument(javax.swing.text.StyledDocument)

getStyledDocument

public StyledDocument getStyledDocument()
Returns the StyledDocument that is the content model for this JTextPane. This is a typed wrapper for JTextComponent.getDocument().

Returns:
the content model of this JTextPane

setStyledDocument

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

Parameters:
document - the content model to set

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.

Overrides:
replaceSelection in class JEditorPane
Parameters:
content - the text with which the selection is replaced

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

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 == null
Parameters:
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

removeStyle

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

Parameters:
nm - the name of the Style to be removed

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

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

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

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

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:
getInputAttributes()

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

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

getInputAttributes

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

Returns:
the attributes that are applied to newly inserted text

getStyledEditorKit

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

Returns:
the current StyledEditorKit of this JTextPane

createDefaultEditorKit

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

Overrides:
createDefaultEditorKit in class JEditorPane
Returns:
the default EditorKit that is used in JTextPanes

setEditorKit

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

Overrides:
setEditorKit in class JEditorPane
Parameters:
editor - the EditorKit to set
Throws:
IllegalArgumentException - if editor is no StyledEditorKit

paramString

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

Overrides:
paramString in class JEditorPane
Returns:
a param string that can be used for debugging.