Document: getDefaultRootElement() : Document « javax.swing.text « Java by API






Document: getDefaultRootElement()

 


import javax.swing.JTextArea;
import javax.swing.text.Element;

public class Main {
  public static void main(String[] argv) throws Exception {
    JTextArea textArea = new JTextArea("word1 word2\nword3\nword4");
    Element paragraph = textArea.getDocument().getDefaultRootElement();
    int contentCount = paragraph.getElementCount();
    for (int i = 0; i < contentCount; i++) {
      Element e = paragraph.getElement(i);
      int rangeStart = e.getStartOffset();
      int rangeEnd = e.getEndOffset();
      String line = textArea.getText(rangeStart, rangeEnd - rangeStart);
      System.out.println(line);
    }
  }
}

   
  








Related examples in the same category

1.Document: addDocumentListener(DocumentListener listener)
2.Document: addUndoableEditListener(UndoableEditListener listener)
3.Document: dump(PrintStream out)
4.Document: getLength()
5.Document: getText(int offset, int length)
6.Document: insertString(int offset, String str, AttributeSet a)
7.Document: remove(int offs, int len)