- All Implemented Interfaces:
- Serializable,- Document,- StyledDocument
- Direct Known Subclasses:
- HTMLDocument
 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.
- See Also:
- 
Nested Class SummaryNested ClassesModifier and TypeClassDescriptionstatic classAn UndoableEdit used to remember AttributeSet changes to an Element.classClass to manage changes to the element hierarchy.static classSpecification for building elements.protected classDefault root element for a document... maps out the paragraphs/lines contained.Nested classes/interfaces declared in class javax.swing.text.AbstractDocumentAbstractDocument.AbstractElement, AbstractDocument.AttributeContext, AbstractDocument.BranchElement, AbstractDocument.Content, AbstractDocument.DefaultDocumentEvent, AbstractDocument.ElementEdit, AbstractDocument.LeafElement
- 
Field SummaryFieldsModifier and TypeFieldDescriptionprotected DefaultStyledDocument.ElementBufferThe element buffer.static final intThe default size of the initial content buffer.Fields declared in class javax.swing.text.AbstractDocumentBAD_LOCATION, BidiElementName, ContentElementName, ElementNameAttribute, listenerList, ParagraphElementName, SectionElementNameFields declared in interface javax.swing.text.DocumentStreamDescriptionProperty, TitleProperty
- 
Constructor SummaryConstructorsConstructorDescriptionConstructs a default styled document.Constructs a styled document.DefaultStyledDocument(StyleContext styles) Constructs a styled document with the default content storage implementation and a shared set of styles.
- 
Method SummaryModifier and TypeMethodDescriptionvoidaddDocumentListener(DocumentListener listener) Adds a document listener for notification of any changes.Adds a new style into the logical style hierarchy.protected voidInitialize the document to reflect the given element structure (i.e. the structure reported by thegetDefaultRootElementmethod.protected AbstractDocument.AbstractElementCreates the root element to be used to represent the default document structure.getBackground(AttributeSet attr) Gets the background color from an attribute set.getCharacterElement(int pos) Gets a character element based on a position.Gets the default root element.getFont(AttributeSet attr) Gets the font from an attribute set.getForeground(AttributeSet attr) Gets the foreground color from an attribute set.getLogicalStyle(int p) Fetches the logical style assigned to the paragraph represented by the given position.getParagraphElement(int pos) Gets the paragraph element at the offsetpos.Fetches a named style previously added.Enumeration<?> Fetches the list of style names.protected voidinsert(int offset, DefaultStyledDocument.ElementSpec[] data) Inserts new elements in bulk.protected voidUpdates document structure as a result of text insertion.voidremoveDocumentListener(DocumentListener listener) Removes a document listener.voidremoveElement(Element elem) Removes an element from this document.voidremoveStyle(String nm) Removes a named style previously added to the document.protected voidUpdates document structure as a result of text removal.voidsetCharacterAttributes(int offset, int length, AttributeSet s, boolean replace) Sets attributes for some part of the document.voidsetLogicalStyle(int pos, Style s) Sets the logical style to use for the paragraph at the given position.voidsetParagraphAttributes(int offset, int length, AttributeSet s, boolean replace) Sets attributes for a paragraph.protected voidstyleChanged(Style style) Called when any of this document's styles have changed.Methods declared in class javax.swing.text.AbstractDocumentaddUndoableEditListener, createBranchElement, createLeafElement, createPosition, dump, fireChangedUpdate, fireInsertUpdate, fireRemoveUpdate, fireUndoableEditUpdate, getAsynchronousLoadPriority, getAttributeContext, getBidiRootElement, getContent, getCurrentWriter, getDocumentFilter, getDocumentListeners, getDocumentProperties, getEndPosition, getLength, getListeners, getProperty, getRootElements, getStartPosition, getText, getText, getUndoableEditListeners, insertString, postRemoveUpdate, putProperty, readLock, readUnlock, remove, removeUndoableEditListener, render, replace, setAsynchronousLoadPriority, setDocumentFilter, setDocumentProperties, writeLock, writeUnlockMethods declared in class java.lang.Objectclone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods declared in interface javax.swing.text.DocumentaddUndoableEditListener, createPosition, getEndPosition, getLength, getProperty, getRootElements, getStartPosition, getText, getText, insertString, putProperty, remove, removeUndoableEditListener, render
- 
Field Details- 
BUFFER_SIZE_DEFAULTpublic static final int BUFFER_SIZE_DEFAULTThe default size of the initial content buffer.- See Also:
 
- 
bufferThe element buffer.
 
- 
- 
Constructor Details- 
DefaultStyledDocumentConstructs a styled document.- Parameters:
- c- the container for the content
- styles- resources and style definitions which may be shared across documents
 
- 
DefaultStyledDocumentConstructs a styled document with the default content storage implementation and a shared set of styles.- Parameters:
- styles- the styles
 
- 
DefaultStyledDocumentpublic DefaultStyledDocument()Constructs a default styled document. This buffers input content by a size of BUFFER_SIZE_DEFAULT and has a style context that is scoped by the lifetime of the document and is not shared with other documents.
 
- 
- 
Method Details- 
getDefaultRootElementGets the default root element.- Specified by:
- getDefaultRootElementin interface- Document
- Specified by:
- getDefaultRootElementin class- AbstractDocument
- Returns:
- the root
- See Also:
 
- 
createInitialize the document to reflect the given element structure (i.e. the structure reported by thegetDefaultRootElementmethod. If the document contained any data it will first be removed.- Parameters:
- data- the element data
 
- 
insertprotected void insert(int offset, DefaultStyledDocument.ElementSpec[] data) throws BadLocationException Inserts new elements in bulk. This is useful to allow parsing with the document in an unlocked state and prepare an element structure modification. This method takes an array of tokens that describe how to update an element structure so the time within a write lock can be greatly reduced in an asynchronous update situation.This method is thread safe, although most Swing methods are not. Please see Concurrency in Swing for more information. - Parameters:
- offset- the starting offset >= 0
- data- the element data
- Throws:
- BadLocationException- for an invalid starting offset
 
- 
removeElementRemoves an element from this document.The element is removed from its parent element, as well as the text in the range identified by the element. If the element isn't associated with the document, IllegalArgumentExceptionis thrown.As empty branch elements are not allowed in the document, if the element is the sole child, its parent element is removed as well, recursively. This means that when replacing all the children of a particular element, new children should be added before removing old children. Element removal results in two events being fired, the DocumentEventfor changes in element structure andUndoableEditEventfor changes in document content.If the element contains end-of-content mark (the last "\n"character in document), this character is not removed; instead, preceding leaf element is extended to cover the character. If the last leaf already ends with"\n",it is included in content removal.If the element is null,NullPointerExceptionis thrown. If the element structure would become invalid after the removal, for example if the element is the document root element,IllegalArgumentExceptionis thrown. If the current element structure is invalid,IllegalStateExceptionis thrown.- Parameters:
- elem- the element to remove
- Throws:
- NullPointerException- if the element is- null
- IllegalArgumentException- if the element could not be removed
- IllegalStateException- if the element structure is invalid
- Since:
- 1.7
 
- 
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.- Specified by:
- addStylein interface- StyledDocument
- Parameters:
- 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.
- Returns:
- the style
 
- 
removeStyleRemoves a named style previously added to the document.- Specified by:
- removeStylein interface- StyledDocument
- Parameters:
- nm- the name of the style to remove
 
- 
getStyleFetches a named style previously added.- Specified by:
- getStylein interface- StyledDocument
- Parameters:
- nm- the name of the style
- Returns:
- the style
 
- 
getStyleNamesFetches the list of style names.- Returns:
- all the style names
 
- 
setLogicalStyleSets the logical style to use for the paragraph at the given 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 turn 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 Concurrency in Swing for more information. - Specified by:
- setLogicalStylein interface- StyledDocument
- Parameters:
- pos- the offset from the start of the document >= 0
- s- the logical style to assign to the paragraph, null if none
 
- 
getLogicalStyleFetches the logical style assigned to the paragraph represented by the given position.- Specified by:
- getLogicalStylein interface- StyledDocument
- Parameters:
- p- the location to translate to a paragraph and determine the logical style assigned >= 0. This is an offset from the start of the document.
- Returns:
- the style, null if none
 
- 
setCharacterAttributesSets attributes for some part of the document. A write lock is held by this operation while changes are being made, and a DocumentEvent is sent to the listeners after the change has been successfully completed.offsetandlengthdefine the range of the text over which the attributes are set. If the length is <= 0, then no action is taken and the method just returns. If the offset is <=0 or > the length of the text then no action is taken, and the method just returns. Otherwise ifoffset + lengthwill exceed the length of the text then the affected range is truncated.This method is thread safe, although most Swing methods are not. Please see Concurrency in Swing for more information. - Specified by:
- setCharacterAttributesin interface- StyledDocument
- Parameters:
- offset- the offset in the document >= 0
- length- the length > 0
- s- the attributes
- replace- true if the previous attributes should be replaced before setting the new attributes
 
- 
setParagraphAttributesSets attributes for a paragraph.This method is thread safe, although most Swing methods are not. Please see Concurrency in Swing for more information. - Specified by:
- setParagraphAttributesin interface- StyledDocument
- Parameters:
- offset- the offset into the paragraph >= 0
- length- the number of characters affected >= 0
- s- the attributes
- replace- whether to replace existing attributes, or merge them
 
- 
getParagraphElementGets the paragraph element at the offsetpos. A paragraph consists of at least one child Element, which is usually a leaf.- Specified by:
- getParagraphElementin interface- StyledDocument
- Specified by:
- getParagraphElementin class- AbstractDocument
- Parameters:
- pos- the starting offset >= 0
- Returns:
- the element
 
- 
getCharacterElementGets a character element based on a position.- Specified by:
- getCharacterElementin interface- StyledDocument
- Parameters:
- pos- the position in the document >= 0
- Returns:
- the element
 
- 
insertUpdateUpdates document structure as a result of text insertion. This will happen within a write lock. This implementation simply parses the inserted content for line breaks and builds up a set of instructions for the element buffer.- Overrides:
- insertUpdatein class- AbstractDocument
- Parameters:
- chng- a description of the document change
- attr- the attributes
 
- 
removeUpdateUpdates document structure as a result of text removal.- Overrides:
- removeUpdatein class- AbstractDocument
- Parameters:
- chng- a description of the document change
 
- 
createDefaultRootCreates the root element to be used to represent the default document structure.- Returns:
- the element base
 
- 
getForegroundGets the foreground color from an attribute set.- Specified by:
- getForegroundin interface- StyledDocument
- Parameters:
- attr- the attribute set
- Returns:
- the color
 
- 
getBackgroundGets the background color from an attribute set.- Specified by:
- getBackgroundin interface- StyledDocument
- Parameters:
- attr- the attribute set
- Returns:
- the color
 
- 
getFontGets the font from an attribute set.- Specified by:
- getFontin interface- StyledDocument
- Parameters:
- attr- the attribute set
- Returns:
- the font
 
- 
styleChangedCalled when any of this document's styles have changed. Subclasses may wish to be intelligent about what gets damaged.- Parameters:
- style- The Style that has changed.
 
- 
addDocumentListenerAdds a document listener for notification of any changes.- Specified by:
- addDocumentListenerin interface- Document
- Overrides:
- addDocumentListenerin class- AbstractDocument
- Parameters:
- listener- the listener
- See Also:
 
- 
removeDocumentListenerRemoves a document listener.- Specified by:
- removeDocumentListenerin interface- Document
- Overrides:
- removeDocumentListenerin class- AbstractDocument
- Parameters:
- listener- the listener
- See Also:
 
 
-