Java Tips Weblog

  • Blog Stats

    • 2,076,564 hits
  • Categories

  • Archives

  • Advertisements

Protected Text Component

Posted by Rob Camick on December 21, 2008

Text components allow you to freely change any of the text displayed in the component. There may be times when you wish to prevent the user from changing specific pieces of text. This is a relatively complex task as you will need to prevent the Document from being updated. As well it would probably be a good idea to highlight the protected text so the user has some visual feedback.

The ProtectedTextComponent class tries to accomplish the above functionality within a single class. This class will coordinate the behaviour of two related classes. The ProtectedDocument class will provide the functionality for protecting the text in the Document and the ProtectedHighlighter class will provide the functionality for highlighting the protected text.

The ProtectedDocument class is not actually a Document, but it will maintain the information necessary to prevent protected text from being updated or deleted. It provides two separate, but related pieces of functionality. A custom:

  • DocumentFilter – is used to control which areas of the text can be updated. Both insertion of new text in a protected region of text and the removal of protected text is prevented.
  • NavigationFilter – is used to control the caret movement. The caret will skip over the protected text and position itself at the next editable text character.

Similiarly, the ProtectedHighlighter class will provide two related pieces of highlighting functionality. It will manage:

  • highlight size – which must remain fixed, since the protected text can’t change. However, default processing in text components is to merge attributes as a character is typed with the previous character. This behaviour causes the highlight size to grow. Therefore, we must manage the highlight size internally so that it doesn’t grow. (Note, if anybody knows a way to prevent the highlight from growing I would love to hear it).
  • painting highlights – we can use the default functionality of the painter, but we need to know when an entire line should be highlighted so that we can force the highlighting to the right edge of the component, not just to the right edge of the text.

Protecting text with code something like this:

ProtectedTextComponent ptc = new ProtectedTextComponent(textComponent);
ptc.protectText(0, 0);
ptc.protectText(9, 10);
ptc.protectText(18, 19);
ptc.protectLines(4, 5);

would result in an image like this:



Note, although the ProtectedDocument class was designed to be used with the ProtectedTextComponent class it can be used as a stand alone class if you don’t need the protected text highlighted.

Get The Code

Related Reading

Implementing a Document Filter
Java API: javax.swing.text.NavigationFilter


Leave a Reply

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

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

Google+ photo

You are commenting using your Google+ 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 )


Connecting to %s

%d bloggers like this: