This discussion is archived
2 Replies Latest reply: Mar 2, 2013 7:40 PM by e-dubkendo RSS

How to get Character under cursor?

e-dubkendo Newbie
Currently Being Moderated
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?
    shakir.gusaroff Expert
    Currently Being Moderated
    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()));
    
                    }
                }
            });
    Update:

    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 {
    
        @Override
        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");
            h.setOnMouseEntered(mouseEntered);
            Text e = new Text("E");
            e.setOnMouseEntered(mouseEntered);
            Text l1 = new Text("L");
            l1.setOnMouseEntered(mouseEntered);
            Text l2 = new Text("L");
            l2.setOnMouseEntered(mouseEntered);
            Text o = new Text("O");
            o.setOnMouseEntered(mouseEntered);
            HBox hbox = new HBox();
            hbox.getChildren().addAll(h, e, l1, l2, o);
    
    
            VBox root = new VBox();
            root.getChildren().addAll(hbox);
    
            Scene scene = new Scene(root, 300, 250);
            primaryStage.setScene(scene);
            primaryStage.show();
        }
    
        public static void main(String[] args) {
            launch(args);
        }
    }
    The next javafx8 release will have a TextFlow to lay out rich text.
    A TextFlow greatly simplifies text processing.

    http://download.java.net/jdk8/jfxdocs/javafx/scene/text/TextFlow.html

    Edited by: shakir.gusaroff on Mar 2, 2013 7:15 PM
  • 2. Re: How to get Character under cursor?
    e-dubkendo Newbie
    Currently Being Moderated
    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?

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points