I am currently trying to solve a problem when I need to find a position in a piece of text in JEditorPane, based on a mouse click.
Basically, when a user right-clicks on a word, I need to find out what the word is. To do this, I need to find out what position in the text the user clicked. I know that I can easily get the mouse position from the MouseEvent, which is passed to the mousePressed method, and I am told that you can convert this to get the index of the character in a piece of text - however, I cannot figure out how to do this.
I tried the viewToModel () method on JEditorPane, but that returns me the wrong position in the text, so either I use it incorrectly or it doesn't work that way.
Any ideas?
java swing cursor jeditorpane
Scottm
source share