OK, obviously I need some extra work on text manipulation in Java. Manipulating text in Java is a double edged sword. On one hand, it is really easy to get started. Java keeps track of the text models and deals with them automatically so you don’t really even have to have a working concept of them. On the other hand, if you want to do anything more serious than what has already been created by someone else, you have to be able to manipulate the model yourself. And, of course, in order to manipulate the model in some other way than throwing things against the wall and seeing what sticks, you actually have to understand what the model is doing.
In order to be a decent programmer, it is important to understand what the code causes the computer to do. That sounds obvious, but often we don’t really understand. Often, we don’t have any better understanding than Pavlov’s dog.  We simply learn that when we do something, something else happens in response. What we don’t know is what the change is doing down in the code to cause those changes to appear.
Most Java Swing components use models: 
Most Swing components have models. A button (JButton), for example, has a model (a ButtonModel object) that stores the button’s state — what its keyboard mnemonic is, whether it’s enabled, selected, or pressed, and so on. Some components have multiple models. A list (JList), for example, uses a ListModel to hold the list’s contents, and a ListSelectionModel to track the list’s current selection.
We don’t need to know all the details of these models. In most cases, we don’t even need to know that the Swing component uses models at all. We learn some API function to call, and Swing takes care of the rest. But that doesn’t work when we really need to customize something like a JTextPane. We not only need to know that it has a model, but how to manipulate it to store the information we want, as well as needing to know how it will effect the view.
From “How to Use Models”: 
Although Swing’s model architecture is sometimes referred to as a Model-View-Controller (MVC) design, it really isn’t. Swing components are generally implemented so that the view and controller are indivisible, implemented by a single UI object provided by the look and feel. The Swing model architecture is more accurately described as a separable model architecture. If you’re interested in learning more about the Swing model architecture, see A Swing Architecture Overview, an article in The Swing Connection.
Now that we have a general idea of what models do, we want to look closer at text models. Here is a list of what text models do: 
JTextComponentclass is the foundation for Swing text components. This class provides the following customizable features for all of its descendants:
- A model, known as a document, that manages the component’s content.
- A view, which displays the component on screen.
- A controller, known as an editor kit, that reads and writes text and implements editing capabilities with actions.
- Support for infinite undo and redo.
- A pluggable caret and support for caret change listeners and navigation filters.
From the above description, you can see where Swing components get the Model-View-Controller (MVC) reference even if it might not be accurate with all Swing components.
A text component’s model is known as a document and is an instance of a class that implements the Document interface. … 
That quote is very important. The Document we are working with is the model for the text component. Whether we realize it or not, it is going to store all the information necessary to format the document the way we want. And if we truly want to manipulate the text, we must know how to manipulate the document (model).
There is only one thing left to point out. While it is true that JTextPane is a descendant of JTextComponent, JTextPane uses a special model called StyledDocument as its model. It needs this expanded model to hold the various text attributes that aren’t present in a regular text document (model).
… A document provides the following services for a text component:
- Contains the text. A document stores the textual content in
objects, which can represent any logical text structure, such as
paragraphs, or text runs that share styles. We do not describe
- Provides support for editing the text through the
- Notifies document listeners and undoable edit listeners of changes to the text.
Positionobjects, which track a particular location within the text even as the text is modified.
- Allows you to obtain information about the text, such as its length, and segments of the text as a string.
The Swing text package contains a subinterface of
StyledDocument, that adds support for marking up the text with styles. One
JTextPane, requires that its document be a
StyledDocumentrather than merely a