Search code examples
javaswingjtextpane

How do I stop JEditorPane from auto-indenting subsequent text?


My program loads an RTF file and displays it in a JEditorPane with the following code:

    public void ReadFile() {
        RTFEditorKit rtfKit = new RTFEditorKit();
        StyledDocument doc = (StyledDocument) rtfKit.createDefaultDocument();
        rcp.getJEditorPane().setEditorKit(rtfKit);

        try {
            FileInputStream fi = new FileInputStream("Document.rtf");
            rtfKit.read(fi, doc, 0);
            rcp.getJEditorPane().setDocument(doc);
        } catch (FileNotFoundException e) {
            System.out.println("File not found");
        } catch (IOException e) {
            System.out.println("I/O error");
        } catch (BadLocationException e) {
        }
    }

Everything is going fine as long as I don't insert any indents, bulleted lists, or numbered lists in the original document. If the document does contain a one of these elements, all subsequent text is indented when displayed in the JEditorPane as well. It's written as follows in the original RTF file:

enter image description here

What I want is for the JEditorPane to display the text as shown above. Instead, it is displayed in the JEditorPane as follows:

enter image description here

Any solution using a TextArea or a TextPane instead of an EditorPane would also suffice.


Solution

  • The fact that I got no answers here was indication that people were unable to reproduce my error, which helped pinpoint the problem.

    It wasn't the code of the program but rather the way in which I kept editing the RTF document that caused the problem. I was using WordPad to add the indents to the document, after which I'd load the document into the program. The indents in WordPad, however, don't register properly, which is what caused the problem.

    I noticed this when I used the following class to edit the RTF document directly inside the program. Saving the program afterwards created proper indentations. I then opened the document in Microsoft Word instead and added another indent, and this too fixed the problem, so the problem is fixed by not using WordPad to edit the RTF document that you wish to load into the program.