Java Reference
In-Depth Information
Figure 3-3.
Sharing MVC data models
Sharing of a data model can be done in either one of two ways:
•
You can create the data model apart from any component and tell each component to
use the data model.
•
You can create one component first, get the model from the first component, and then
share it with the other components.
Listing 3-1 demonstrates how to share a data model using the latter technique.
Listing 3-1.
Sharing an MVC Model
import java.awt.*;
import javax.swing.*;
import javax.swing.text.*;
public class ShareModel {
public static void main (String args[]) {
Runnable runner = new Runnable() {
public void run() {
JFrame frame = new JFrame("Sharing Sample");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container content = frame.getContentPane();
JTextArea textarea1 = new JTextArea();
Document document = textarea1.getDocument();
JTextArea textarea2 = new JTextArea(document);
JTextArea textarea3 = new JTextArea(document);
content.setLayout(new BoxLayout(content, BoxLayout.Y_AXIS));
content.add(new JScrollPane(textarea1));