The Swing Editing Framework

A good reference on text components is the Swing Tutorial:

http://java.sun.com/docs/books/tutorial/uiswing/components/text.html

Here's a partial overview of the framework:

Note that models are called documents in the editing framework.

Example: Writer's Desktop

Version 1.0 of Writer's Desktop uses plain text editing, but does support copy, cut, and paste. It also supports multiple views that update themselves automatically:

Design

The design uses a desktop containing multiple internal frames. Each internal frame contains a text area. All text areas share a model (document) provided by the main frame:

Implementation

Here are some of the imports we will need:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.*;
import javax.swing.event.*;
import javax.swing.text.*;

An editor frame contains a text area with a specified document:

class EditorFrame extends JInternalFrame {
   private static final long serialVersionUID = 1L;
   private static int openFrameCount = 0;
   public EditorFrame(Document model) {
      super("View #" + (++openFrameCount),
           true, //resizable
           true, //closable
           true, //maximizable
           true);//iconifiable
      JTextArea view = new JTextArea(5, 30);
      view.setDocument(model);
      JScrollPane scrollPane = new JScrollPane(view);
      setContentPane(scrollPane);

      pack();
   }
}

The desktop is very similar to other desktops. The main difference is that when internal frames are created, they are editor frames:

public class WriterDesktop extends JFrame
   implements Runnable, ActionListener {

   private static final long serialVersionUID = 1L;
   private JDesktopPane desktop;
   private Document model;

   public WriterDesktop(Document model) {
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      setDefaultLookAndFeelDecorated(true);
      desktop = new JDesktopPane(); //a specialized layered pane
      this.model = model;
      setJMenuBar(createMenuBar());
      createFrame(); //create first "window"
      setContentPane(desktop);
      //Make dragging a little faster but perhaps uglier.
      desktop.setDragMode(JDesktopPane.OUTLINE_DRAG_MODE);
   }

   protected void createFrame() {
      JInternalFrame frame = new EditorFrame(model);
      frame.setVisible(true);
      desktop.add(frame);
      try {
         frame.setSelected(true);
      } catch (java.beans.PropertyVetoException e) {}
   }

   protected JMenuBar createMenuBar() { /* as before */ }
   public void run()() { /* as before */ }
   public void display()() { /* as before */ }
   public void actionPerformed(ActionEvent e) { /* as before */ }

   public static void main(String[] args) {
      Document model = new PlainDocument();
      WriterDesktop app = new WriterDesktop(model);
      app.setTitle("Application Desktop");
      app.display();
   }
}