When changes are made to a
Document, observers of the
Document are notified by the event types
UndoableEditEvent, defined in the
UndoableEditEvent and its associated listener
interface are discussed in Chapter
18. In this section, we’ll look at
DocumentEvent and its relatives.
The DocumentListener Interface
Document uses this interface to notify its registered listeners
of changes. It calls one of three methods depending on the category of
change and passes a
to specify the details.
- public void changedUpdate(DocumentEvent e)
Signal that an attribute or set of attributes has changed for some of the
Document’s content. The
DocumentEventspecifies exactly which part of the
- public void insertUpdate(DocumentEvent e)
Signal that text has been inserted into the
DocumentEventspecifies which part of the
Document’s content is new.
- public void removeUpdate(DocumentEvent e)
Signal that text has been removed from the
DocumentEventspecifies where the text was located in the
Documentbefore it was deleted.
Suppose we want the parentheses matcher we wrote in the last
section to update its colors “live” instead of waiting for the user
to click on a button. All we have to do is register with the pane’s
Document as a
DocumentListener. Whenever we’re notified
that text has been inserted or deleted, we recolor the parentheses.
It’s that easy.
We do have to be careful not to ...