Java Reference
In-Depth Information
Figure 3-1. MVC communication mechanism
UI Delegates for Swing Components
This example demonstrates an important aspect of the MVC architecture within the Swing
world. Complex interactions need to happen between the view and the controller. The Swing
design combines these two elements into a delegate object to simplify the overall design. This
results in each Swing component having a UI delegate that is in charge of rendering the current
state of the component and dealing with user input events.
Sometimes, the user events result in changes to the view that don't affect the model. For
instance, the cursor position is an attribute of the view. The model doesn't care about the posi-
tion of the cursor, only the text contents. User input that affects the cursor position isn't passed
along to the model. At other times, user input that affects the contents of the Document (for
example, pressing the Backspace key) is passed along. Pressing the Backspace key results in a
character being removed from the model. Because of this tight coupling, each Swing component
has a UI delegate.
To demonstrate, Figure 3-2 shows the makeup of the JTextArea , with respect to the model
and UI delegate. The UI delegate for the JTextArea starts with the TextUI interface, with
its basic implementation in the BasicTextUI class. In turn, this is specialized with the
BasicTextAreaUI for the JTextArea . The BasicTextAreaUI creates a view that is either a
PlainView or a WrappedPlainView . On the model side, things are much simpler. The Document
interface is implemented by the AbstractDocument class, which is further specialized by the
PlainDocument .
The text components will be explained more fully in Chapters 15 and 16. As the diagram in
Figure 3-2 demonstrates, much is involved in working with the text components. In most cases,
you don't need to deal with the specifics to the degree shown in this figure. However, all of
these classes are working behind the scenes. The UI-delegate part of the MVC architecture will
be discussed further in Chapter 20, when we explore how to customize delegates.
 
Search WWH ::




Custom Search