Methods Summary |
---|
public javax.swing.text.Style | addStyle(java.lang.String nm, javax.swing.text.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.
StyledDocument doc = getStyledDocument();
return doc.addStyle(nm, parent);
|
protected javax.swing.text.EditorKit | createDefaultEditorKit()Creates the EditorKit to use by default. This
is implemented to return javax.swing.text.StyledEditorKit .
return new StyledEditorKit();
|
public javax.swing.text.AttributeSet | getCharacterAttributes()Fetches the character attributes in effect at the
current location of the caret, or null .
StyledDocument doc = getStyledDocument();
Element run = doc.getCharacterElement(getCaretPosition());
if (run != null) {
return run.getAttributes();
}
return null;
|
public javax.swing.text.MutableAttributeSet | getInputAttributes()Gets the input attributes for the pane.
return getStyledEditorKit().getInputAttributes();
|
public javax.swing.text.Style | getLogicalStyle()Fetches the logical style assigned to the paragraph represented
by the current position of the caret, or null .
StyledDocument doc = getStyledDocument();
return doc.getLogicalStyle(getCaretPosition());
|
public javax.swing.text.AttributeSet | getParagraphAttributes()Fetches the current paragraph attributes in effect
at the location of the caret, or null if none.
StyledDocument doc = getStyledDocument();
Element paragraph = doc.getParagraphElement(getCaretPosition());
if (paragraph != null) {
return paragraph.getAttributes();
}
return null;
|
public javax.swing.text.Style | getStyle(java.lang.String nm)Fetches a named non-null style previously added.
StyledDocument doc = getStyledDocument();
return doc.getStyle(nm);
|
public javax.swing.text.StyledDocument | getStyledDocument()Fetches the model associated with the editor.
return (StyledDocument) getDocument();
|
protected final javax.swing.text.StyledEditorKit | getStyledEditorKit()Gets the editor kit.
return (StyledEditorKit) getEditorKit();
|
public java.lang.String | getUIClassID()Returns the class ID for the UI.
return uiClassID;
|
public void | insertComponent(java.awt.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.
This method is thread safe, although most Swing methods
are not. Please see
How
to Use Threads for more information.
MutableAttributeSet inputAttributes = getInputAttributes();
inputAttributes.removeAttributes(inputAttributes);
StyleConstants.setComponent(inputAttributes, c);
replaceSelection(" ", false);
inputAttributes.removeAttributes(inputAttributes);
|
public void | insertIcon(javax.swing.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
How
to Use Threads for more information.
MutableAttributeSet inputAttributes = getInputAttributes();
inputAttributes.removeAttributes(inputAttributes);
StyleConstants.setIcon(inputAttributes, g);
replaceSelection(" ", false);
inputAttributes.removeAttributes(inputAttributes);
|
protected java.lang.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 .
return super.paramString();
|
public void | removeStyle(java.lang.String nm)Removes a named non-null style previously added to
the document.
StyledDocument doc = getStyledDocument();
doc.removeStyle(nm);
|
public void | replaceSelection(java.lang.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.
This method is thread safe, although most Swing methods
are not. Please see
How
to Use Threads for more information.
replaceSelection(content, true);
|
private void | replaceSelection(java.lang.String content, boolean checkEditable)
if (checkEditable && !isEditable()) {
UIManager.getLookAndFeel().provideErrorFeedback(JTextPane.this);
return;
}
Document doc = getStyledDocument();
if (doc != null) {
try {
Caret caret = getCaret();
int p0 = Math.min(caret.getDot(), caret.getMark());
int p1 = Math.max(caret.getDot(), caret.getMark());
AttributeSet attr = getInputAttributes().copyAttributes();
if (doc instanceof AbstractDocument) {
((AbstractDocument)doc).replace(p0, p1 - p0, content,attr);
}
else {
if (p0 != p1) {
doc.remove(p0, p1 - p0);
}
if (content != null && content.length() > 0) {
doc.insertString(p0, content, attr);
}
}
} catch (BadLocationException e) {
UIManager.getLookAndFeel().provideErrorFeedback(JTextPane.this);
}
}
|
public void | setCharacterAttributes(javax.swing.text.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
How
to Use Threads for more information.
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);
}
|
public void | setDocument(javax.swing.text.Document doc)Associates the editor with a text document. This
must be a StyledDocument .
if (doc instanceof StyledDocument) {
super.setDocument(doc);
} else {
throw new IllegalArgumentException("Model must be StyledDocument");
}
|
public final void | setEditorKit(javax.swing.text.EditorKit kit)Sets the currently installed kit for handling
content. This is the bound property that
establishes the content type of the editor.
if (kit instanceof StyledEditorKit) {
super.setEditorKit(kit);
} else {
throw new IllegalArgumentException("Must be StyledEditorKit");
}
|
public void | setLogicalStyle(javax.swing.text.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.
This method is thread safe, although most Swing methods
are not. Please see
How
to Use Threads for more information.
StyledDocument doc = getStyledDocument();
doc.setLogicalStyle(getCaretPosition(), s);
|
public void | setParagraphAttributes(javax.swing.text.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
How
to Use Threads for more information.
int p0 = getSelectionStart();
int p1 = getSelectionEnd();
StyledDocument doc = getStyledDocument();
doc.setParagraphAttributes(p0, p1 - p0, attr, replace);
|
public void | setStyledDocument(javax.swing.text.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.
super.setDocument(doc);
|
private void | writeObject(java.io.ObjectOutputStream s)See readObject and writeObject in
JComponent for more
information about serialization in Swing.
s.defaultWriteObject();
if (getUIClassID().equals(uiClassID)) {
byte count = JComponent.getWriteObjCounter(this);
JComponent.setWriteObjCounter(this, --count);
if (count == 0 && ui != null) {
ui.installUI(this);
}
}
|