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
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.
| Methods from java.awt.Component: |
|---|
|
action, add, addComponentListener, addFocusListener, addHierarchyBoundsListener, addHierarchyListener, addInputMethodListener, addKeyListener, addMouseListener, addMouseMotionListener, addMouseWheelListener, addNotify, addPropertyChangeListener, addPropertyChangeListener, applyComponentOrientation, areFocusTraversalKeysSet, bounds, checkImage, checkImage, coalesceEvents, contains, contains, createImage, createImage, createVolatileImage, createVolatileImage, deliverEvent, disable, disableEvents, dispatchEvent, dispatchEventImpl, doLayout, enable, enable, enableEvents, enableInputMethods, eventTypeEnabled, findNextFocusComponent, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, generateName, getAccessibleContext, getAlignmentX, getAlignmentY, getBackground, getBounds, getBounds, getColorModel, getComponentAt, getComponentAt, getComponentListeners, getComponentOrientation, getCursor, getDropTarget, getFocusCycleRootAncestor, getFocusListeners, getFocusTraversalKeys, getFocusTraversalKeysEnabled, getFont, getFontMetrics, getForeground, getGraphics, getGraphicsConfiguration, getGraphicsConfigurationImpl, getHeight, getHierarchyBoundsListeners, getHierarchyListeners, getIgnoreRepaint, getInputContext, getInputMethodListeners, getInputMethodRequests, getKeyListeners, getListeners, getLocale, getLocation, getLocation, getLocationOnScreen, getMaximumSize, getMinimumSize, getMouseListeners, getMouseMotionListeners, getMouseWheelListeners, getName, getParent, getPeer, getPreferredSize, getPropertyChangeListeners, getPropertyChangeListeners, getSize, getSize, getToolkit, getTreeLock, getWidth, getX, getY, gotFocus, handleEvent, hasFocus, hide, imageUpdate, inside, invalidate, isBackgroundSet, isCursorSet, isDisplayable, isDoubleBuffered, isEnabled, isFocusCycleRoot, isFocusOwner, isFocusTraversable, isFocusable, isFontSet, isForegroundSet, isLightweight, isOpaque, isShowing, isValid, isVisible, keyDown, keyUp, layout, list, list, list, list, list, locate, location, lostFocus, minimumSize, mouseDown, mouseDrag, mouseEnter, mouseExit, mouseMove, mouseUp, move, nextFocus, paint, paintAll, paramString, postEvent, preferredSize, prepareImage, prepareImage, print, printAll, processComponentEvent, processEvent, processFocusEvent, processHierarchyBoundsEvent, processHierarchyEvent, processInputMethodEvent, processKeyEvent, processMouseEvent, processMouseMotionEvent, processMouseWheelEvent, remove, removeComponentListener, removeFocusListener, removeHierarchyBoundsListener, removeHierarchyListener, removeInputMethodListener, removeKeyListener, removeMouseListener, removeMouseMotionListener, removeMouseWheelListener, removeNotify, removePropertyChangeListener, removePropertyChangeListener, repaint, repaint, repaint, repaint, requestFocus, requestFocus, requestFocusInWindow, requestFocusInWindow, reshape, resize, resize, setBackground, setBounds, setBounds, setComponentOrientation, setCursor, setDropTarget, setEnabled, setFocusTraversalKeys, setFocusTraversalKeysEnabled, setFocusable, setFont, setForeground, setIgnoreRepaint, setLocale, setLocation, setLocation, setName, setPeer, setSize, setSize, setVisible, show, show, size, toString, transferFocus, transferFocusBackward, transferFocusUpCycle, translateEvent, update, validate |
| Method from javax.swing.JTextPane Detail: |
public Style addStyle(String nm,
Style parent) {
StyledDocument doc = getStyledDocument();
return doc.addStyle(nm, 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. |
protected EditorKit createDefaultEditorKit() {
return new StyledEditorKit();
}
Creates the EditorKit to use by default. This
is implemented to return javax.swing.text.StyledEditorKit. |
public AttributeSet getCharacterAttributes() {
StyledDocument doc = getStyledDocument();
Element run = doc.getCharacterElement(getCaretPosition());
if (run != null) {
return run.getAttributes();
}
return null;
}
Fetches the character attributes in effect at the
current location of the caret, or null. |
public MutableAttributeSet getInputAttributes() {
return getStyledEditorKit().getInputAttributes();
}
Gets the input attributes for the pane. |
public Style getLogicalStyle() {
StyledDocument doc = getStyledDocument();
return doc.getLogicalStyle(getCaretPosition());
}
Fetches the logical style assigned to the paragraph represented
by the current position of the caret, or null. |
public AttributeSet getParagraphAttributes() {
StyledDocument doc = getStyledDocument();
Element paragraph = doc.getParagraphElement(getCaretPosition());
if (paragraph != null) {
return paragraph.getAttributes();
}
return null;
}
Fetches the current paragraph attributes in effect
at the location of the caret, or null if none. |
public Style getStyle(String nm) {
StyledDocument doc = getStyledDocument();
return doc.getStyle(nm);
}
Fetches a named non-null style previously added. |
public StyledDocument getStyledDocument() {
return (StyledDocument) getDocument();
}
Fetches the model associated with the editor. |
protected final StyledEditorKit getStyledEditorKit() {
return (StyledEditorKit) getEditorKit();
}
|
public String getUIClassID() {
return uiClassID;
}
Returns the class ID for the UI. |
public void insertComponent(Component c) {
MutableAttributeSet inputAttributes = getInputAttributes();
inputAttributes.removeAttributes(inputAttributes);
StyleConstants.setComponent(inputAttributes, c);
replaceSelection(" ", false);
inputAttributes.removeAttributes(inputAttributes);
}
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. |
public void insertIcon(Icon g) {
MutableAttributeSet inputAttributes = getInputAttributes();
inputAttributes.removeAttributes(inputAttributes);
StyleConstants.setIcon(inputAttributes, g);
replaceSelection(" ", false);
inputAttributes.removeAttributes(inputAttributes);
}
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. |
protected String paramString() {
return super.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. |
public void removeStyle(String nm) {
StyledDocument doc = getStyledDocument();
doc.removeStyle(nm);
}
Removes a named non-null style previously added to
the document. |
public void replaceSelection(String content) {
replaceSelection(content, true);
}
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. |
public void setCharacterAttributes(AttributeSet attr,
boolean replace) {
int p0 = getSelectionStart();
int p1 = getSelectionEnd();
if (p0 != p1) {
StyledDocument doc = getStyledDocument();
doc.setCharacterAttributes(p0, p1 - p0, attr, replace);
} else {
MutableAttributeSet inputAttributes = getInputAttributes();
if (replace) {
inputAttributes.removeAttributes(inputAttributes);
}
inputAttributes.addAttributes(attr);
}
}
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. |
public void setDocument(Document doc) {
if (doc instanceof StyledDocument) {
super.setDocument(doc);
} else {
throw new IllegalArgumentException("Model must be StyledDocument");
}
}
Associates the editor with a text document. This
must be a StyledDocument. |
public final void setEditorKit(EditorKit kit) {
if (kit instanceof StyledEditorKit) {
super.setEditorKit(kit);
} else {
throw new IllegalArgumentException("Must be StyledEditorKit");
}
}
Sets the currently installed kit for handling
content. This is the bound property that
establishes the content type of the editor. |
public void setLogicalStyle(Style s) {
StyledDocument doc = getStyledDocument();
doc.setLogicalStyle(getCaretPosition(), 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. |
public void setParagraphAttributes(AttributeSet attr,
boolean replace) {
int p0 = getSelectionStart();
int p1 = getSelectionEnd();
StyledDocument doc = getStyledDocument();
doc.setParagraphAttributes(p0, p1 - p0, attr, 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. |
public void setStyledDocument(StyledDocument doc) {
super.setDocument(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. |