Java Reference
In-Depth Information
// Define attribute set ”greenText” and ...
SimpleAttributeSet greenText = new SimpleAttributeSet();
// ... set its attributes to green font
StyleConstants.setForeground(greenText, Color.green);
// Format 15 symbols beginning with 200 using ”greenText”.
// Any previously assigned attributes except the text colour
// are maintained.
styledDoc.setCharacterAttributes(203,15,greenText, false );
When a text range is formatted, position markers are automatically assigned to its
beginning and end. These markers - not the values for offset and length specified
!
in setCharacterAttributes - determine the range of the formatting henceforth.
Thus, the formatted range will move left or right when text is deleted or inserted
before it. Symbols inserted into the formatted range receive the same formatting,
thus extending the range.
18.3
Document listeners
When documents are not merely displayed but edited by the user, the application
might want to react to changes in the document. One might, for example, want
to change the formatting of certain text ranges or protocol the changes made to
be able to undo them or to document the development process. In order to
react to modifications in the document, one uses a listener, in this case a
DocumentListener .
Whenever changes are made to a Document , which has a DocumentListener
assigned to it, the listener is informed. The changes include deletions and inser-
tions of text or modifications in the layout. The runtime system then generates an
instance of DocumentEvent that contains information on the kind and location of
the change. This event is passed to the listener.
To implement the interface DocumentListener , one has to implement the fol-
lowing three methods. They are called by the runtime system when text is inserted,
deleted or formatting attributes are changed:
public void insertUpdate( DocumentEvent devt)
public void removeUpdate( DocumentEvent devt)
public void changedUpdate(DocumentEvent devt)
The instance devt of class DocumentEvent contains information on the event. The
class provides the following methods to access this information:
Document getDocument()
int getLength()
int getOffset()
DocumentEvent.EventType getType()
DocumentEvent.Element getChange(Element elem)
Search WWH ::




Custom Search