Chapter 20. Document Model and Events

In the previous chapter, we introduced the Swing text components, ignoring many of the details in order to simplify the introduction. In this chapter,[46] we’ll give a detailed explanation of the many classes and interfaces that make up the basic text model. Additionally, we’ll look at the classes and interfaces involved in delivering events to provide notification of document model changes.

The Document Model

Before getting into the details of the classes and interfaces that make up the Swing text model, we’ll give a brief overview of the top-level interfaces that serve as the model’s key abstractions. These interfaces, and the relationships between them, are shown in Figure 20.1.

High-level Document class diagram

Figure 20-1. High-level Document class diagram

The first—and arguably most important—interface to look at is called Document. The model representation of any text component (even a simple text field) in Swing is defined by this interface. A Document is little more than an arbitrary collection of text (though Swing’s implementations of the interface provide much more).

Document objects are partitioned by Element s that describe the structural pieces of a Document, such as paragraphs or sections of specially formatted text (e.g., italics). Elements are made up of child Elements, forming a tree with a “root” Element as the base and arbitrary levels of child Elements below ...

Get Java Swing now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.