Text Manipulation in Java – JTextComponents

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. [1] 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: [2]

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”: [2]

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: [3]

The JTextComponent class 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. … [3]

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 Element
    objects, which can represent any logical text structure, such as
    paragraphs, or text runs that share styles. We do not describe Element objects here.
  • Provides support for editing the text through the remove and insertString methods.
  • Notifies document listeners and undoable edit listeners of changes to the text.
  • Manages Position objects, 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 Document,
StyledDocument, that adds support for marking up the text with styles. One JTextComponent subclass, JTextPane, requires that its document be a StyledDocument rather than merely a Document.[3]

[1] Classical Conditioning – Wikipedia
[2] How to Use Models – Oracle Java Tutorial
[3] Text Component Features – Oracle Java Tutorials

Advertisements

Published by

Marisa

I am a writer of words, a thinker of thoughts, a changer of genders, and a queerer of life. I am an antagonist of the ordinary; and while I do tolerate it, I also look at it with contempt.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s