Skip to Main Content
Java Swing, 2nd Edition
book

Java Swing, 2nd Edition

by Dave Wood, Robert Eckstein, Marc Loy, James Elliott, Brian Cole
November 2002
Intermediate to advanced content levelIntermediate to advanced
1278 pages
38h 26m
English
O'Reilly Media, Inc.
Content preview from Java Swing, 2nd Edition

Document Events

When changes are made to a Document, observers of the Document are notified by the event types DocumentEvent and UndoableEditEvent, defined in the javax.swing.event package. 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 DocumentEvent to specify the details.

Methods

public void changedUpdate(DocumentEvent e)

Signal that an attribute or set of attributes has changed for some of the Document’s content. The DocumentEvent specifies exactly which part of the Document is affected.

public void insertUpdate(DocumentEvent e)

Signal that text has been inserted into the Document. The DocumentEvent specifies which part of the Document’s content is new.

public void removeUpdate(DocumentEvent e)

Signal that text has been removed from the Document. The DocumentEvent specifies where the text was located in the Document before 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 ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Java Threads, 3rd Edition

Java Threads, 3rd Edition

Scott Oaks, Henry Wong

Publisher Resources

ISBN: 0596004087Errata PageSupplemental Content