1 answer
If you look at the TextFieldSkin class, it inherits from TextInputControlSkin . It contains an instance of the Path class, which shows the cursor, caretPath . But it is declared with the protected modifier.
protected final Path caretPath = new Path(); To gain access to it, you need to create a successor. In it we will get access to our cursor. And with each change of the cursor we will catch it and process it.
import com.sun.javafx.scene.control.skin.TextFieldSkin; import javafx.beans.value.ChangeListener; import javafx.beans.value.ObservableValue; import javafx.geometry.Bounds; import javafx.geometry.Point2D; import javafx.scene.control.TextField; import javafx.stage.Stage; public class TextFieldCaretControlSkin extends TextFieldSkin { public TextFieldCaretControlSkin(TextField textField, Stage stage) { super(textField); // ΡΠ»Π΅Π΄ΠΈΠΌ Π·Π° ΠΈΠ·ΠΌΠ΅Π½Π΅Π½ΠΈΡΠΌΠΈ layoutBoundProperty Π½Π°ΡΠ΅Π³ΠΎ ΡΠΊΠ·Π΅ΠΌΠΏΠ»ΡΡΠ° Path caretPath.layoutBoundsProperty().addListener(new ChangeListener<Bounds>() { @Override public void changed(ObservableValue<? extends Bounds> observable, Bounds oldValue, Bounds newValue) { // ΠΊΠΎΠΎΡΠ΄ΠΈΠ½Π°ΡΡ ΠΊΡΡΡΠΎΡΠ° ΠΎΡΠ½ΠΎΡΠΈΡΠ΅Π»ΡΠ½ΠΎ TextField-Π° double x = newValue.getMaxX(); double y = newValue.getMaxY(); // ΠΈΡΠΊΠΎΠΌΠ°Ρ ΠΊΠΎΠΎΡΠ΄ΠΈΠ½Π°ΡΠ° Point2D p = caretPath.localToScene(x, y); System.out.println(p.toString()); } }); } } The same way you can get the coordinates of the cursor from all TextInputControlSkin .
- After finding the location of the cursor, you can add different
Popups, add animations, etc. next to the cursor. - catscoolzhyk
|
