I was trying to make an editor component that included the same functionalities that you would see in editing softwares. Functions like bold, underline, italicize, left align and others. The component I used is a JTextPane and since the code deals with RTF, I used an RTFEditorKit as the EditorKit.

Common functions like bold, underline, italicize, left-right-center align, cut, copy and paste are very simple since Action classes are already part of DefaultEditorKit and RTFEditorKit respectively. However, I was stumped when I tried to incorporate bullet and numbered lists. It seems they are not supported in RTFEditorKit. If the JTextPane would have HTML formatting, it would have been possible but since the format is in RTF, the only possible way for this to happen is to create my own.

Well, I did not. It is not an easy task. I would have to familiarize myself with a bevy of other classes. I did manage to find a working sample of a bullet and numbered list but the code based itself on HTML content. Oh well … the last alternative would be to buy a commercial 3rd party library of an editor.

I also learned that it was not the Java Dev Team responsible for creating the RTF support. And unless they are going to add this feature in the next JDK version (7), I will consider the RTFEditorKit class as incomplete.

An RTF document contains a set of code when you view it in a text editor, codes that only RTF readers and clients can understand. Replacing contents in an RTF document can be tricky. The source code in this post converts new line characters into pilcrow symbols and space characters as dot symbols (see image).

The source code was made by my friend sumpix. To correctly make use of this class, make sure that the RTFEditorKit uses a DefaultStyledDocument as its document. The JTextPane object passed to the RTFEditorKit class will have a KeyListener that when ALT-P is pressed, conversion of new lines and space characters to their intended symbols will occur. Any key pressed after that will convert it back to its original.

To use the class, do this:

Get the customized RTFEditorKit class here.

Related Posts Plugin for WordPress, Blogger...