问题
I am using Utilities.getRowStart
to find out the number of lines in a JTextPane
. But it gives the BadLocationException
when I hit the enter key:
javax.swing.text.BadLocationException: Position not represented by view
Any idea?
int offset = pane.getText().length();
while(offset > 0) {
try {
offset = Utilities.getRowStart(pane, offset) - 1;
} catch (BadLocationException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
lineCount++;
}
回答1:
int offset = pane.getText().length();
Just a guess that you are working on Windows. That code will return a string containing "\r\n" for every newline character. The Document only uses "\n" so your offset will be greater than the length of the document. Use:
int offset = pane.getDocument().getLength();
来源:https://stackoverflow.com/questions/15719770/badlocationexception-when-using-utilities-getrowstart-on-hit-of-enter-key