- 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
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:
- StyledEditorKit
- 
Nested Class SummaryNested classes/interfaces declared in class javax.swing.JEditorPaneJEditorPane.AccessibleJEditorPane, JEditorPane.AccessibleJEditorPaneHTML, JEditorPane.JEditorPaneAccessibleHypertextSupportNested classes/interfaces declared in class javax.swing.text.JTextComponentJTextComponent.AccessibleJTextComponent, JTextComponent.DropLocation, JTextComponent.KeyBindingNested classes/interfaces declared in class java.awt.ComponentComponent.AccessibleAWTComponent, Component.BaselineResizeBehavior, Component.BltBufferStrategy, Component.FlipBufferStrategy
- 
Field SummaryFields declared in class javax.swing.JComponentlistenerList, TOOL_TIP_TEXT_KEY, ui, UNDEFINED_CONDITION, WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, WHEN_FOCUSED, WHEN_IN_FOCUSED_WINDOWFields declared in class java.awt.ComponentaccessibleContext, BOTTOM_ALIGNMENT, CENTER_ALIGNMENT, LEFT_ALIGNMENT, RIGHT_ALIGNMENT, TOP_ALIGNMENTFields declared in interface java.awt.image.ImageObserverABORT, ALLBITS, ERROR, FRAMEBITS, HEIGHT, PROPERTIES, SOMEBITS, WIDTH
- 
Constructor SummaryConstructors Constructor Description JTextPane()Creates a newJTextPane.JTextPane(StyledDocument doc)Creates a newJTextPane, with a specified document model.
- 
Method SummaryModifier and Type Method Description StyleaddStyle(String nm, Style parent)Adds a new style into the logical style hierarchy.protected EditorKitcreateDefaultEditorKit()Creates theEditorKitto use by default.AttributeSetgetCharacterAttributes()Fetches the character attributes in effect at the current location of the caret, ornull.MutableAttributeSetgetInputAttributes()Gets the input attributes for the pane.StylegetLogicalStyle()Fetches the logical style assigned to the paragraph represented by the current position of the caret, ornull.AttributeSetgetParagraphAttributes()Fetches the current paragraph attributes in effect at the location of the caret, ornullif none.StylegetStyle(String nm)Fetches a named non-nullstyle previously added.StyledDocumentgetStyledDocument()Fetches the model associated with the editor.protected StyledEditorKitgetStyledEditorKit()Gets the editor kit.StringgetUIClassID()Returns the class ID for the UI.voidinsertComponent(Component c)Inserts a component into the document as a replacement for the currently selected content.voidinsertIcon(Icon g)Inserts an icon into the document as a replacement for the currently selected content.protected StringparamString()Returns a string representation of thisJTextPane.voidremoveStyle(String nm)Removes a named non-nullstyle previously added to the document.voidreplaceSelection(String content)Replaces the currently selected content with new content represented by the given string.voidsetCharacterAttributes(AttributeSet attr, boolean replace)Applies the given attributes to character content.voidsetDocument(Document doc)Associates the editor with a text document.voidsetEditorKit(EditorKit kit)Sets the currently installed kit for handling content.voidsetLogicalStyle(Style s)Sets the logical style to use for the paragraph at the current caret position.voidsetParagraphAttributes(AttributeSet attr, boolean replace)Applies the given attributes to paragraphs.voidsetStyledDocument(StyledDocument doc)Associates the editor with a text document.Methods declared in class javax.swing.JEditorPaneaddHyperlinkListener, createEditorKitForContentType, fireHyperlinkUpdate, getAccessibleContext, getContentType, getEditorKit, getEditorKitClassNameForContentType, getEditorKitForContentType, getHyperlinkListeners, getPage, getPreferredSize, getScrollableTracksViewportHeight, getScrollableTracksViewportWidth, getStream, getText, read, registerEditorKitForContentType, registerEditorKitForContentType, removeHyperlinkListener, scrollToReference, setContentType, setEditorKitForContentType, setPage, setPage, setTextMethods declared in class javax.swing.text.JTextComponentaddCaretListener, addKeymap, copy, cut, fireCaretUpdate, getActions, getCaret, getCaretColor, getCaretListeners, getCaretPosition, getDisabledTextColor, getDocument, getDragEnabled, getDropLocation, getDropMode, getFocusAccelerator, getHighlighter, getKeymap, getKeymap, getMargin, getNavigationFilter, getPreferredScrollableViewportSize, getPrintable, getScrollableBlockIncrement, getScrollableUnitIncrement, getSelectedText, getSelectedTextColor, getSelectionColor, getSelectionEnd, getSelectionStart, getText, getToolTipText, getUI, isEditable, loadKeymap, modelToView, modelToView2D, moveCaretPosition, paste, print, print, print, read, removeCaretListener, removeKeymap, restoreComposedText, saveComposedText, select, selectAll, setCaret, setCaretColor, setCaretPosition, setDisabledTextColor, setDragEnabled, setDropMode, setEditable, setFocusAccelerator, setHighlighter, setKeymap, setMargin, setNavigationFilter, setSelectedTextColor, setSelectionColor, setSelectionEnd, setSelectionStart, setUI, updateUI, viewToModel, viewToModel2D, writeMethods declared in class javax.swing.JComponentaddAncestorListener, addNotify, addVetoableChangeListener, computeVisibleRect, contains, createToolTip, disable, enable, firePropertyChange, firePropertyChange, fireVetoableChange, getActionForKeyStroke, getActionMap, getAlignmentX, getAlignmentY, getAncestorListeners, getAutoscrolls, getBaseline, getBaselineResizeBehavior, getBorder, getBounds, getClientProperty, getComponentGraphics, getComponentPopupMenu, getConditionForKeyStroke, getDebugGraphicsOptions, getDefaultLocale, getFontMetrics, getGraphics, getHeight, getInheritsPopupMenu, getInputMap, getInputMap, getInputVerifier, getInsets, getInsets, getListeners, getLocation, getMaximumSize, getMinimumSize, getNextFocusableComponent, getPopupLocation, getRegisteredKeyStrokes, getRootPane, getSize, getToolTipLocation, getToolTipText, getTopLevelAncestor, getTransferHandler, getVerifyInputWhenFocusTarget, getVetoableChangeListeners, getVisibleRect, getWidth, getX, getY, grabFocus, hide, isDoubleBuffered, isLightweightComponent, isManagingFocus, isOpaque, isOptimizedDrawingEnabled, isPaintingForPrint, isPaintingOrigin, isPaintingTile, isRequestFocusEnabled, isValidateRoot, paint, paintBorder, paintChildren, paintComponent, paintImmediately, paintImmediately, print, printAll, printBorder, printChildren, printComponent, processComponentKeyEvent, processKeyBinding, processKeyEvent, processMouseEvent, 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, setFocusTraversalKeys, setFont, setForeground, setInheritsPopupMenu, setInputMap, setInputVerifier, setMaximumSize, setMinimumSize, setNextFocusableComponent, setOpaque, setPreferredSize, setRequestFocusEnabled, setToolTipText, setTransferHandler, setUI, setVerifyInputWhenFocusTarget, setVisible, unregisterKeyboardAction, updateMethods declared in class java.awt.Containeradd, 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, isFocusCycleRoot, isFocusTraversalPolicyProvider, isFocusTraversalPolicySet, layout, list, list, locate, minimumSize, paintComponents, preferredSize, printComponents, processContainerEvent, processEvent, remove, remove, removeAll, removeContainerListener, setComponentZOrder, setFocusCycleRoot, setFocusTraversalPolicy, setFocusTraversalPolicyProvider, setLayout, transferFocusDownCycle, validate, validateTreeMethods declared in class java.awt.Componentaction, add, addComponentListener, addFocusListener, addHierarchyBoundsListener, addHierarchyListener, addInputMethodListener, 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, getForeground, getGraphicsConfiguration, getHierarchyBoundsListeners, getHierarchyListeners, getIgnoreRepaint, getInputContext, getInputMethodListeners, getInputMethodRequests, getKeyListeners, getLocale, getLocation, getLocationOnScreen, getMouseListeners, getMouseMotionListeners, getMousePosition, getMouseWheelListeners, getName, getParent, getPropertyChangeListeners, getPropertyChangeListeners, getSize, getToolkit, getTreeLock, gotFocus, handleEvent, hasFocus, 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, processMouseWheelEvent, remove, removeComponentListener, removeFocusListener, removeHierarchyBoundsListener, removeHierarchyListener, removeInputMethodListener, removeKeyListener, removeMouseListener, removeMouseMotionListener, removeMouseWheelListener, removePropertyChangeListener, removePropertyChangeListener, repaint, repaint, repaint, requestFocus, requestFocus, requestFocusInWindow, resize, resize, setBounds, setBounds, setComponentOrientation, setCursor, setDropTarget, setFocusable, setFocusTraversalKeysEnabled, setIgnoreRepaint, setLocale, setLocation, setLocation, setMixingCutoutShape, setName, setSize, setSize, show, show, size, toString, transferFocus, transferFocusBackward, transferFocusUpCycle
- 
Constructor Details- 
JTextPanepublic JTextPane()Creates a newJTextPane. A new instance ofStyledEditorKitis created and set, and the document model set tonull.
- 
JTextPaneCreates a newJTextPane, with a specified document model. A new instance ofjavax.swing.text.StyledEditorKitis created and set.- Parameters:
- doc- the document model
 
 
- 
- 
Method Details- 
getUIClassIDReturns the class ID for the UI.- Overrides:
- getUIClassIDin class- JEditorPane
- Returns:
- the string "TextPaneUI"
- See Also:
- JComponent.getUIClassID(),- UIDefaults.getUI(javax.swing.JComponent)
 
- 
setDocumentAssociates the editor with a text document. This must be aStyledDocument.- Overrides:
- setDocumentin class- JTextComponent
- Parameters:
- doc- the document to display/edit
- Throws:
- IllegalArgumentException- if- doccan't be narrowed to a- StyledDocumentwhich is the required type of model for this text component
- See Also:
- JTextComponent.getDocument()
 
- 
setStyledDocumentAssociates 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
 
- 
getStyledDocumentFetches the model associated with the editor.- Returns:
- the model
 
- 
replaceSelectionReplaces 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:
- replaceSelectionin class- JEditorPane
- Parameters:
- content- the content to replace the selection with
 
- 
insertComponentInserts 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 methodJComponent.setAlignmentY. For example, setting a value of0.75will 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
 
- 
insertIconInserts 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- the icon to insert
- See Also:
- Icon
 
- 
addStyleAdds 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- nullif 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- nullif unspecified attributes need not be resolved in some other style.
- Returns:
- the new Style
 
- 
removeStyleRemoves a named non-nullstyle previously added to the document.- Parameters:
- nm- the name of the style to remove
 
- 
getStyleFetches a named non-nullstyle previously added.- Parameters:
- nm- the name of the style
- Returns:
- the Style
 
- 
setLogicalStyleSets 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- the logical style to assign to the paragraph, or- nullfor no style
 
- 
getLogicalStyleFetches the logical style assigned to the paragraph represented by the current position of the caret, ornull.- Returns:
- the Style
 
- 
getCharacterAttributesFetches the character attributes in effect at the current location of the caret, ornull.- Returns:
- the attributes, or null
 
- 
setCharacterAttributesApplies 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- the attributes
- replace- if true, then replace the existing attributes first
 
- 
getParagraphAttributesFetches the current paragraph attributes in effect at the location of the caret, ornullif none.- Returns:
- the attributes
 
- 
setParagraphAttributesApplies 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- the non-- nullattributes
- replace- if true, replace the existing attributes first
 
- 
getInputAttributesGets the input attributes for the pane.- Returns:
- the attributes
 
- 
getStyledEditorKitGets the editor kit.- Returns:
- the editor kit
 
- 
createDefaultEditorKitCreates theEditorKitto use by default. This is implemented to returnjavax.swing.text.StyledEditorKit.- Overrides:
- createDefaultEditorKitin class- JEditorPane
- Returns:
- the editor kit
 
- 
setEditorKitSets the currently installed kit for handling content. This is the bound property that establishes the content type of the editor.- Overrides:
- setEditorKitin class- JEditorPane
- Parameters:
- kit- the desired editor behavior
- Throws:
- IllegalArgumentException- if kit is not a- StyledEditorKit
- See Also:
- JEditorPane.getEditorKit()
 
- 
paramStringReturns a string representation of thisJTextPane. 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 benull.- Overrides:
- paramStringin class- JEditorPane
- Returns:
- a string representation of this JTextPane
 
 
-