I want to write an event handler for a TextArea which upon the user clicking on the TextArea can determine the index of the character clicked and act based on this.
The suggested solutions in Get cursor position (in characters) within a text Input field are Javascript instead of Java and do not answer how to convert mouse click coordinates to a text index.
Currently I have:
private EventHandler<MouseEvent> mouseHandler = new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent mouseEvent) {
int x=(int)mouseEvent.getX();
int y=(int)mouseEvent.getY();
String type=mouseEvent.getEventType().toString();
if(type=="MOUSE_CLICKED")
{
System.out.println("Mouse clicked over textarea at x="+x+" y="+y);
// TODO
// determine text index under click
}
}
};
TextArea my_textarea=new TextArea();
my_textarea.setOnMouseClicked(mouseHandler);