Convert Mouse Position To Caret Position In A JTextComponent
Posted by tech on
January 20, 2011
Converting a mouse position to the caret position of a JTextComponent can be done by using the JTextComponent method viewToModel(). This can be pretty useful if you need to set the caret position of the component when you do a right click of the mouse.
The component can apply to JTextArea, JEditorPane and JTextPane respectively.
1 2 | Point p = mouseEvent.getPoint(); textpane.setCaretPosition(textpane.viewToModel(p)); |








