2017-02-27 6 views
0

DefaultStyledDocument에 추가 한 네 개의 단락을 제거하려고합니다. 하지만 기대했던대로 행동하지 않습니다.Swing DefaultStyledDocument traversal

내가 뭘 잘못하고 있니? 여기에 전체 코드를 추가했습니다. 요청 된대로입니다.

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 
+0

:

테스트되지 않은 코드는 무언가 같이 수 있습니다. 또한 예외를 유발 한 행이 코드의 어느 행인지 설명하십시오. – VGR

+0

_not은 내가 기대하는대로 동작하지 않도록 정의해야합니다. 여기서 무엇을 달성하려고합니까? – Nicolas

답변

2

몇 가지 : 루프 아무 의미도하지 않습니다 당신의

  1. 변수. 한 번에 한 줄의 텍스트를 읽고 각 줄의 크기를 미리 알지 못합니다. getText(...) 방법

  2. 매개 변수는

당신은 텍스트의 라인을 얻을 수있는 Document에서 Element 클래스를 사용할 수 있습니다 잘못. 당신이 예외의 스택 트레이스의 나머지 부분을 왼쪽으로 나타납니다

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); 
} 
+0

수정 및 올바른 제공. –

+0

@wax_lyrical, 이것이 도움이된다면 사람들이 문제가 해결되었다는 것을 알기 위해 체크 표시를 클릭하여 답변을 "수락"하는 것을 잊지 마십시오. – camickr

+0

확실히 도움이되었지만, 저에게 알려 주신 것이 있으시면 Documents and EditorKits에 대한 더 많은 문서를 사용할 수 있습니다. 감사 –