Fred Kleinschmidt
2013-02-22 21:52:23 UTC
I am getting a BadLocationException from a call to textpane.modelToView()
when the position I pass should be legal.
I create a JTextPane and get its document:
JTextPane jtp = new JTextPane();
Document doc = jtp.getDocument();
Then I add a document listener to the document.
This is inside a DocumentListener's insertUpdate() method:
public void insertUpdate( DocumentEvent event ) {
Document doc = event.getDocument();
int len = doc.getLength();
if ( len > 0 ) {
try {
Rectangle view = jtp.modelToView(len-1);
// do other things here
} catch (Exception e) {
e.printStackTrace();
}
}
}
Everything is fine until I enter the first character after a newline.
Then I get a BadLocationException: Position not represented by view
(I am using Java 1.6, on a Windows 7 platform)
when the position I pass should be legal.
I create a JTextPane and get its document:
JTextPane jtp = new JTextPane();
Document doc = jtp.getDocument();
Then I add a document listener to the document.
This is inside a DocumentListener's insertUpdate() method:
public void insertUpdate( DocumentEvent event ) {
Document doc = event.getDocument();
int len = doc.getLength();
if ( len > 0 ) {
try {
Rectangle view = jtp.modelToView(len-1);
// do other things here
} catch (Exception e) {
e.printStackTrace();
}
}
}
Everything is fine until I enter the first character after a newline.
Then I get a BadLocationException: Position not represented by view
(I am using Java 1.6, on a Windows 7 platform)