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.

I was fooling around with the Label control in Visual Studio and I wanted the next few words to be on the 2nd line of the Label control. \n is a newline character and I expected that adding that one to the Label’s Text property, the newline would take effect. Turns out the slash and n appears as is if you do it in Visual Studio’s property box. To have a newline working in a Label Control, you would have to do it programatically. Then your \n will work.

This may hold true with other controls. If your newline character will not work when you add it within Visual Studio’s property box, try adding it programatically. I am sure it will work.

Related Posts Plugin for WordPress, Blogger...