Having a JEditorPane support RTF is possible with the use of an editor kit class. In Java’s case, the RTFEditorKit does the trick. When you save RTF files and view them in a text editor, you would see codes that start with \ plus some letters. Only RTF clients understand it. In the JEditorPane’s case, the RTFEditorKit will be the one to convert the RTF code into the JEditorPane’s document and display the same content as what you would see in an RTF client application.

There is no need to call the setText() method of JEditorPane. Once the RTFEditorKit stores and converts the RTF code to String into the javax.swing.text.Document of the JEditorPane, it will automatically be displayed.

The method returns a String just for convenience in case you want to know what the content is. Or, you can call doc.getText(0, doc.getLength()) directly after the RTFEditorKit has finished processing the Document object.

Related Posts Plugin for WordPress, Blogger...