2 Replies Latest reply on Mar 3, 2013 3:40 AM by e-dubkendo

    How to get Character under cursor?

      I am trying to find the best approach/strategy for getting the character under the cursor in a text node containing multiple characters and wondered if anyone had any idea how to go about doing this?
        • 1. Re: How to get Character under cursor?
          Hi. You can get the character at the cursor in the text field by adding a listener to the caretPosition property:
                  final TextField tf1 = new TextField();
                  tf1.caretPositionProperty().addListener(new ChangeListener<Number>() {
                      public void changed(ObservableValue<? extends Number> observable, Number oldValue, final Number newValue) {
                          if (newValue != null && newValue.intValue() > 0) {
                              System.out.println("The caracter at cursor is --> " + tf1.getText(newValue.intValue() - 1, newValue.intValue()));

          You are asking about text, not a text field. I did not pay close attention. Sorry about that.
          It can be done for text too.
          import javafx.application.Application;
          import javafx.beans.value.ChangeListener;
          import javafx.beans.value.ObservableValue;
          import javafx.event.ActionEvent;
          import javafx.event.EventHandler;
          import javafx.scene.Cursor;
          import javafx.scene.Scene;
          import javafx.scene.control.*;
          import javafx.scene.effect.InnerShadow;
          import javafx.scene.input.KeyCode;
          import javafx.scene.input.KeyEvent;
          import javafx.scene.input.MouseEvent;
          import javafx.scene.layout.*;
          import javafx.scene.text.Text;
          import javafx.stage.Stage;
          public class TextFieldSample extends Application {
              public void start(Stage primaryStage) {
                  EventHandler<MouseEvent> mouseEntered = new EventHandler<MouseEvent>() {
                      public void handle(MouseEvent event) {
                          System.out.println(((Text) event.getSource()).getText());
                  Text h = new Text("H");
                  Text e = new Text("E");
                  Text l1 = new Text("L");
                  Text l2 = new Text("L");
                  Text o = new Text("O");
                  HBox hbox = new HBox();
                  hbox.getChildren().addAll(h, e, l1, l2, o);
                  VBox root = new VBox();
                  Scene scene = new Scene(root, 300, 250);
              public static void main(String[] args) {
          The next javafx8 release will have a TextFlow to lay out rich text.
          A TextFlow greatly simplifies text processing.


          Edited by: shakir.gusaroff on Mar 2, 2013 7:15 PM
          • 2. Re: How to get Character under cursor?
            Thanks, so it is as I feared, you have to make each character its own node? I'm going to go look over the docs you linked, but will the new TextFlow make it possible to take an approach more like your TextField example?