简体   繁体   English

如何在未聚焦之前获得TextField的最后一个插入符号position?

[英]How to get last caret position of a TextField before unfocus?

I tried to make a button that can insert a special character into a TextField, and this is my original implementation:我试图制作一个可以在 TextField 中插入特殊字符的按钮,这是我最初的实现:

// input is a TextField
input.insertText(input.getCaretPosition(), "∞");

But I found that it always insert the text to the start, then after some tries, I found that "unfocus" the TextField will make caretPosition goes to 0. And that isn't what I want, so I tried to use directly listen to caretPosition property like this:但是我发现它总是将文本插入到开头,然后经过一些尝试,我发现TextField的“unfocus”会使caretPosition变为0。这不是我想要的,所以我尝试使用直接听caretPosition属性如下:

AtomicInteger caretPos = new AtomicInteger();
input.caretPositionProperty().addListener((obs, oldVal, newVal) -> {
    caretPos.set(newVal.intValue());
    System.out.println(newVal.intValue());
});

But the problem is that the listen will still be called when losing focus, which resets caretPos to 0 again.但问题是失去焦点时仍然会调用listen,这会将caretPos再次重置为0。

Simply avoid updating caretPos if the text field has lost focus:如果文本字段失去焦点,只需避免更新caretPos

AtomicInteger caretPos = new AtomicInteger();
input.caretPositionProperty().addListener((obs, oldVal, newVal) -> {
    if (input.isFocused()) {
        caretPos.set(newVal.intValue());
        System.out.println(newVal.intValue());
    }
});

Here's a complete working example:这是一个完整的工作示例:

import java.util.concurrent.atomic.AtomicInteger;

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class TextFieldFocusTest extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception {

        TextField input = new TextField();
        Button insertInfinity = new Button("∞");


        AtomicInteger caretPos = new AtomicInteger();
        input.caretPositionProperty().addListener((obs, oldVal, newVal) -> {
            if (input.isFocused()) {
                caretPos.set(newVal.intValue());
            }
        });

        insertInfinity.setOnAction(e -> {
            int pos = caretPos.get();
            input.insertText(pos, "∞");
            input.requestFocus();
            input.positionCaret(pos+1);
        });

        VBox root = new VBox(10);
        root.setPadding(new Insets(10,10,10,10));
        root.setAlignment(Pos.CENTER);
        root.getChildren().addAll(input, insertInfinity);
        Scene scene = new Scene(root);
        primaryStage.setScene(scene);
        primaryStage.setWidth(400);
        primaryStage.setHeight(400);
        primaryStage.show();
    }

    public static void main(String[] args) {
        Application.launch(args);
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM