public class JTextPane extends JEditorPane
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.
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
.
StyledEditorKit
JEditorPane.AccessibleJEditorPane, JEditorPane.AccessibleJEditorPaneHTML, JEditorPane.JEditorPaneAccessibleHypertextSupport
JTextComponent.AccessibleJTextComponent, JTextComponent.DropLocation, JTextComponent.KeyBinding
JComponent.AccessibleJComponent
Container.AccessibleAWTContainer
Component.AccessibleAWTComponent, Component.BaselineResizeBehavior, Component.BltBufferStrategy, Component.FlipBufferStrategy
HONOR_DISPLAY_PROPERTIES, W3C_LENGTH_UNITS
DEFAULT_KEYMAP, FOCUS_ACCELERATOR_KEY
listenerList, TOOL_TIP_TEXT_KEY, ui, UNDEFINED_CONDITION, WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, WHEN_FOCUSED, WHEN_IN_FOCUSED_WINDOW
accessibleContext, BOTTOM_ALIGNMENT, CENTER_ALIGNMENT, LEFT_ALIGNMENT, RIGHT_ALIGNMENT, TOP_ALIGNMENT
ABORT, ALLBITS, ERROR, FRAMEBITS, HEIGHT, PROPERTIES, SOMEBITS, WIDTH
public JTextPane()
Creates a new JTextPane
. A new instance of StyledEditorKit
is created and set, and the document model set to null
.
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.
doc
- the document modelpublic String getUIClassID()
Returns the class ID for the UI.
getUIClassID
in class JEditorPane
JComponent.getUIClassID()
, UIDefaults.getUI(javax.swing.JComponent)
public void setDocument(Document doc)
Associates the editor with a text document. This must be a StyledDocument
.
setDocument
in class JTextComponent
doc
- the document to display/editIllegalArgumentException
- if doc
can't be narrowed to a StyledDocument
which is the required type of model for this text componentJTextComponent.getDocument()
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.
doc
- the document to display/editpublic StyledDocument getStyledDocument()
Fetches the model associated with the editor.
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 at the point of insertion. If the document is not editable, beep and return.
replaceSelection
in class JEditorPane
content
- the content to replace the selection withpublic 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.
c
- the component to insertpublic 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.
g
- the icon to insertIcon
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.
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.Style
public void removeStyle(String nm)
Removes a named non-null
style previously added to the document.
nm
- the name of the style to removepublic Style getStyle(String nm)
Fetches a named non-null
style previously added.
nm
- the name of the styleStyle
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.
s
- the logical style to assign to the paragraph, or null
for no stylepublic Style getLogicalStyle()
Fetches the logical style assigned to the paragraph represented by the current position of the caret, or null
.
Style
public AttributeSet getCharacterAttributes()
Fetches the character attributes in effect at the current location of the caret, or null
.
null
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.
attr
- the attributesreplace
- if true, then replace the existing attributes firstpublic AttributeSet getParagraphAttributes()
Fetches the current paragraph attributes in effect at the location of the caret, or null
if none.
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.
attr
- the non-null
attributesreplace
- if true, replace the existing attributes firstpublic MutableAttributeSet getInputAttributes()
Gets the input attributes for the pane.
protected final StyledEditorKit getStyledEditorKit()
Gets the editor kit.
protected EditorKit createDefaultEditorKit()
Creates the EditorKit
to use by default. This is implemented to return javax.swing.text.StyledEditorKit
.
createDefaultEditorKit
in class JEditorPane
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.
setEditorKit
in class JEditorPane
kit
- the desired editor behaviorIllegalArgumentException
- if kit is not a StyledEditorKit
JEditorPane.getEditorKit()
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
.
paramString
in class JEditorPane
JTextPane
© 1993–2017, Oracle and/or its affiliates. All rights reserved.
Documentation extracted from Debian's OpenJDK Development Kit package.
Licensed under the GNU General Public License, version 2, with the Classpath Exception.
Various third party code in OpenJDK is licensed under different licenses (see Debian package).
Java and OpenJDK are trademarks or registered trademarks of Oracle and/or its affiliates.