1

I would like to implement a text input using javafx. The cursor looks like a black rectangle (for people with low vision). It seems impossible with a text area or a text field. The mouse shape can be an arrow for example. Can you give ideas to implement this function ?

newBreach
  • 11
  • 1
  • I have not tested [this](https://github.com/thejeed/javafx-caret-example/tree/master/src/java/de/jeed/caretexample), but [this](https://github.com/thejeed/javafx-caret-example/tree/master/src/java/de/jeed/caretexample) project seems to change the color and width. – SedJ601 Dec 18 '17 at 20:48

1 Answers1

1

enter image description here
Here is full example

import com.sun.javafx.scene.control.skin.TextFieldSkin;
    import javafx.application.Application;
    import javafx.geometry.Insets;
    import javafx.scene.Scene;
    import javafx.scene.control.TextField;
    import javafx.scene.layout.VBox;
    import javafx.scene.paint.Color;
    import javafx.stage.Stage;

    public class CaretColorizer extends Application {

        @Override
        public void start(Stage stage) throws Exception {
            TextField redCaretTextField = new TextField("Big black caret");
            redCaretTextField.setSkin(
                    new TextFieldCaretControlSkin(
                            redCaretTextField,
                            Color.RED
                    )
            );

            VBox layout = new VBox(10, redCaretTextField);

            layout.setPadding(new Insets(10));
            stage.setScene(new Scene(layout));

            stage.show();
        }

        public class TextFieldCaretControlSkin extends TextFieldSkin {
            public TextFieldCaretControlSkin(TextField textField, Color caretColor) {
                super(textField);


                caretPath.strokeProperty().unbind();
                caretPath.fillProperty().unbind();
                caretPath.setStrokeWidth(10);
                caretPath.setStroke(Color.BLACK);
                caretPath.setFill(Color.BLACK);
            }
        }

        public static void main(String[] args) {
            launch(args);
        }
    }
Maksym Rudenko
  • 706
  • 5
  • 16
  • It works, it works but the cursor overlays the two close characters. It is possible to see them between two flashs. In fact the cursor width is only one-pixel. I can implement a space character. Do you have another idea ? – newBreach Dec 23 '17 at 07:52
  • Just add `caretPath.setTranslateX(10);` – Maksym Rudenko Dec 24 '17 at 13:44