KiloJKilo KiloJKilo - 1 year ago 152
Java Question

load RTF into JTextPane

I created a class of type JTextPane in my text editor program. it has a subclass of text and richtext that inherts from my main JTextPaneClass. However, I'm unable to load RTF into my richtext because the method of reading fileinput stream isn't in the superclass JTextPane. So how do I read rich text into jtextpane? This seems very simple I must be missing something. I see lots of examples using RTFEditorKit and filling into the JTextPane but not when its instantiated as a class.

public class RichTextEditor extends TextEditorPane {

private final String extension = ".rtf";
private final String filetype = "text/richtext";

public RichTextEditor() {
// super( null, "", "Untitled", null );
// this.setContentType( "text/richtext" );

* Constructor for tabs with content.
* @param stream
* @param path
* @param fileName
* @param color
public RichTextEditor( FileInputStream stream, String path, String fileName, Color color, boolean saveEligible ) {
super( path, fileName, color, saveEligible );
//RTFEditorKit rtf = new RTFEditorKit();
//this.setEditorKit( rtf );
setEditor();, this.getDocument(), 0);
// stream, "RTFEditorKit" );
this.getDocument().putProperty( "file name", fileName );

private void setEditor() {
this.setEditorKit( new RTFEditorKit() );


the line:, this.getDocument(), 0);

tells me

The method read(InputStream, Document) in the type JEditorPane is not applicable for the arguments (FileInputStream, Document, int)

Answer Source

To be able to access your editor kit, you should keep a reference to it. In fact, your setEditor() method's name is setXXX so this should be a setter (in fact, I'm not convinced that you need to set it more than once, so it may be that this method should not exist at all). Define a field:

private RTFEditorKit kit = new RTFEditorKit();

Then in the constructor,

setEditorKit( kit );;

If you insist on keeping the method, its code should be

kit = new RTFEditorKit();
setEditorKit( kit );

And if you use this from the constructor, remember to set kit to void initially so as not to create an extra object that will be immediately discarded.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download