I have a JTextPane
and using a MouseAdapter
I need to get the position ofthe char that is clicked on. Using viewToModel
does return the wanted position when I click directly over chars, however, it returns the position of the last char in the row when I click in the empty area of the JTextPane
.
Does anyone know how I can avoid getting the position of the last char when I don't click on chars?
Hers's the code that gets the position:
public void mouseClicked(MouseEvent e) {
JTextPane editor = (JTextPane) e.getSource();
Point pt = new Point(e.getX(), e.getY());
int pos = editor.viewToModel(pt);
}