import com.sun.javafx.scene.control.skin.TextAreaSkin; import com.sun.javafx.scene.text.HitInfo; /****************************************************/ TextAreaSkin skin = (TextAreaSkin) target.getSkin(); HitInfo mouseHit = skin.getIndex(e.getX(), e.getY()); // Now you can position caret skin.positionCaret(mouseHit, false, false); // And/or get insertion index int insertionPoint = mouseHit.getInsertionIndex();
For TextArea this method is equivalent to Rolands answer. The practical difference of this method is its applicability to TextField (another subclass of TextInputControl ):
TextFieldSkin skin = (TextFieldSkin) target.getSkin(); HitInfo mouseHit = skin.getIndex(e.getX(), e.getY()); skin.positionCaret(mouseHit, false); int insertionPoint = mouseHit.getInsertionIndex();
Unfortunately, TextFieldSkin does not override getInsertionPoint(...) , and the parent implementation returns 0, so an alternative solution does not work here.
As for Java 9, both Rolands and my answers will still work. The packages com.sun.javafx.scene.control.skin and com.sun.javafx.scene.text (where the HitInfo class is located) are moved to the Java 9 open API. Their locations will be javafx.scene.control.skin and javafx.scene.text , respectively. See Javadocs for JavaFX 9 here: http://download.java.net/java/jdk9/jfxdocs/index.html
source share