Strange modelToView problem...
I need to get position of caret in my jTextPane with monospaced font.
I use following code:
...
private FontMetrics fontMetrics=jTextPane.getFontMetrics(font);
privatestaticint fontWidth=fontMetrics.charWidth(' ');
privatestaticint offset;
try
{
offset=jTextPane.modelToView(0).x;
}
catch (BadLocationException badlocationexception){}
jTextPane.getCaret().addChangeListener(this);
...
publicvoid stateChanged(ChangeEvent e)
{
textPosition=jTextPane.getCaretPosition();
Element rootElemrnt=doc.getDefaultRootElement();
lineCount=rootElemrnt.getElementCount();
coordY=rootElemrnt.getElementIndex(textPosition);
try
{
coordX=(jTextPane.modelToView(textPosition).x-offset)/fontWidth;
}
catch(BadLocationException e1){}
statusBar.setText(" Ln "+(coordY+1)+", Col "+(coordX+1));
}
...
Every time when I press Enter in my jTextPane I have BadLocationException. I don't know why?

