0

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
wax_lyrical
  • 922
  • 1
  • 10
  • 22
  • It appears you left out the rest of the exception’s stack trace. Also, please explain which line of your code is the line which triggered the exception. – VGR Feb 27 '17 at 16:00
  • You should define _not behaving as I would expect_ what are you trying to acheive here ? – Nicolas Feb 27 '17 at 19:23

1 Answers1

2

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);
}
camickr
  • 321,443
  • 19
  • 166
  • 288
  • @wax_lyrical, if this helped, then don't forget to "accept" the answer by clicking on the checkmark so people know the problem has been solved. – camickr Feb 27 '17 at 16:42
  • It sure helped - but I could use more documentation for Documents and EditorKits, if you have anything please let me know. Thanks – wax_lyrical Feb 27 '17 at 16:53