Search code examples
javaswingstyleddocument

Swing DefaultStyledDocument traversal


I'm trying to pull out the four paragraphs that I've added to a DefaultStyledDocument. But it's not behaving as I would expect.

What am I doing wrong? I've added the full code here - as that was what was requested.

import javax.swing.*;
import javax.swing.text.BadLocationException;
import javax.swing.text.DefaultStyledDocument;
import javax.swing.text.Element;

 public class MainFrame extends JFrame {

 JTextPane jTextPane = new JTextPane();

public static void main(String[] args) {
    new MainFrame().init();
    try {
        Thread.sleep(95000);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}

private void init() {
    JFrame frame = new JFrame();
    frame.setSize(1000, 800);
    frame.setVisible(true);

    jTextPane.setSize(995, 795);
    frame.add(jTextPane);

    DefaultStyledDocument document = new DefaultStyledDocument();

    try {
        document.insertString(document.getLength(), "DDDD\n", null);
        document.insertString(document.getLength(), "CCCC\n", null);
        document.insertString(document.getLength(), "BBBB\n", null);
        document.insertString(document.getLength(), "AAAA\n", null);
    } catch (BadLocationException e) {
        e.printStackTrace();
    }

    document.dump(System.out);
    jTextPane.setDocument(document);

    for (int x = 0; x < 20; x += 5) {
        Element paraGE = document.getParagraphElement(x);
        int startOff = paraGE.getStartOffset();
        int endOff = paraGE.getEndOffset();
        String s = null;
        try {
            s = document.getText(startOff, endOff);
        } catch (BadLocationException e) {
            e.printStackTrace();
        }
        System.out.println(s);
    }
}
}

javax.swing.text.BadLocationException: Invalid location
at javax.swing.text.GapContent.getChars(GapContent.java:189)
at javax.swing.text.GapContent.getString(GapContent.java:167)
at javax.swing.text.AbstractDocument.getText(AbstractDocument.java:770)
at blah.MainFrame.init(MainFrame.java:60)
at blah.MainFrame.main(MainFrame.java:14)
javax.swing.text.BadLocationException: Invalid location
at javax.swing.text.GapContent.getChars(GapContent.java:189)
at javax.swing.text.GapContent.getString(GapContent.java:167)
at javax.swing.text.AbstractDocument.getText(AbstractDocument.java:770)
at blah.MainFrame.init(MainFrame.java:60)
at blah.MainFrame.main(MainFrame.java:14)
null
null

Solution

  • A couple of things:

    1. The variables in your for loop make no sense. You want to read one line of text at a time and you don't know in advance what the size of each line might be.

    2. the parameters for the getText(...) method are wrong

    You can use the Element class from the Document to get lines of text.

    Untested code might be something like:

    Element root = textPane.getDocument().getDefaultRootElement();
    int lines = root.getElementCount();
    
    for (int i = 0; i < lines; i++)
    {
        Element line = root.getElement( i ); 
        int start = line.getStartOffset();
        int end = line.getEndOffset();
        String text = document.getText(start, end - start);
        System.out.println(text);
    }