Java Reference
In-Depth Information
getDocument() returns a reference to the Document that triggered the event. This
is useful if the listener monitors more than one document.
getLength() returns the number of symbols affected by the event.
getOffset() returns the offset (position) of the first symbol affected by the event.
getType() returns the type of the event. Using method toString of class Event-
Type this can be turned into text; the possible results are CHANGE , INSERT or
REMOVE .
getChange(Element elem) - instances of class Element allow access to the text
attributes that changed. A number of further classes are involved in this. We
do not discuss the mechanism here; refer to the Java documentation.
18.4
Class DefaultStyledDocument
As already mentioned in the beginning of the chapter, class DefaultStyled-
Document is an implementation of interface Document . Both are found in the
javax.swing.text package. We present only a few methods of this powerful class.
DefaultStyledDocument()
Position createPosition(int offset)
addDocumentListener(DocumentListener docList)
setCharacterAttributes(int offset, int length,
AttributeSet attribSet, boolean replace)
String getText(int offset,int length)
insertString(int offset, Stringtext, AttributeSet attrSet)
remove(int offset,int length)
DefaultStyledDocument() is the constructor.
createPosition(int offset) generates a position marker, i.e. an instance of
Position , before the symbol currently is at position offset .Ifthe parameters
are outside the current size of the document then a BadLocationException
will occur.
addDocumentListener(DocumentListener docList) assigns docList as docu-
ment listener to this document.
setCharacterAttributes - see the discussion in Section 18.2.
String getText(int offset, int length) returns a string consisting of as
many symbols as the current value of length , starting at position offset in
the document. If the parameters are outside the current size of the document
then a BadLocationException will occur.
Search WWH ::




Custom Search