Java Reference
In-Depth Information
content.add(new JScrollPane(textarea2));
content.add(new JScrollPane(textarea3));
frame.setSize (300, 400);
frame.setVisible (true);
}
};
EventQueue.invokeLater(runner);
}
}
Figure 3-4 shows how this program might look after editing the shared document. Notice
that the three text areas are capable of viewing (or modifying) different areas of the document.
They aren't limited to adding text only at the end, for instance. This is because each text area
manages the position and cursor separately. The position and cursor are attributes of the view,
not the model.
Figure 3-4. Sharing a document between JTextArea components
Understanding the Predefined Data Models
When working with Swing components, it's helpful to understand the data models behind
each of the components because the data models store their state. Understanding the data
model for each component helps you to separate the parts of the component that are visual
(and thus part of the view) from those that are logical (and thus part of the data model). For
example, by understanding this separation, you can see why the cursor position within a
JTextArea isn't part of the data model, but rather is part of the view.
Table 3-1 provides a complete listing of the Swing components, the interface that describes
the data model for each component, as well as the specific implementations. If a component
isn't listed, that component inherits its data model from its parent class, most likely
 
Search WWH ::




Custom Search