Javafx 检测文本字段更改并确定插入符位置
Javafx detect textfield change and determine caret position
我正在 Javafx 中实现自动完成逻辑。 TextField
用于用户输入,每次更新时,都会触发侦听器。然后一个 listView
的下拉菜单将显示匹配的结果。
所以问题是,当 textProperty
侦听器正在处理时,我如何才能在 TextField
中获取更新的插入符号位置?
我目前正在使用 textfield.getCaretPosition()
,但它只会 return 之前的位置。
如有任何意见,我们将不胜感激!
commandTextField.textProperty().addListener(new ChangeListener<String>() {
@Override
public void changed(ObservableValue<? extends String> observable,
String oldText, String newText) {
// Following line will return previous caret position
commandTextField.getCaretPosition();
...//autocomplete logic omitted
}
});
编辑:
我需要获取插入符位置的原因是我需要知道输入更改的位置并相应地显示建议。
textField.caretPositionProperty().addListener((ChangeListener<Number>) (observable, oldValue, newValue) -> {
caretPosition = newValue.intValue();
});
textField.textProperty().addListener((ChangeListener<String>) (observable, oldValue, newValue) -> {
...//autocomplete Logic
}
在上述情况下,textProperty
侦听器将首先被调用,然后是 caretPositionProperty
,因此在我的情况下无法获得更新的插入符号位置。
向 caretPositionProperty 添加侦听器。
import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.scene.Scene;
import javafx.scene.control.TextField;
import javafx.stage.Stage;
public class TextFieldCaretApp extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage stage) throws Exception {
TextField textField = new TextField();
Scene scene = new Scene(textField);
stage.setScene(scene);
stage.show();
textField.caretPositionProperty().addListener((ChangeListener<Number>) (observable, oldValue, newValue) -> {
System.out.println("caretPosition=" + newValue);
});
textField.textProperty().addListener((ChangeListener<String>) (observable, oldValue, newValue) -> {
System.out.println("text=" + newValue);
});
}
}
这不是 API 的预期用途,但您可以使用以 UnaryOperator<TextFormatter.Change>
作为参数构造的 TextFormatter
。这通常用于 prevent/modify 对文本的意外更改,但在这种情况下,我们将仅使用 Change
对象来获取您想要的数据。
以下示例 "suggests" 种可能性,每种可能性都用某个字符串替换所选部分:
@Override
public void start(Stage primaryStage) throws Exception {
ListView<String> listView = new ListView<>();
String[] replacements = { "foo", "bar", "42", "zap" };
listView.getItems().setAll(replacements);
TextField textField = new TextField();
TextFormatter<?> formatter = new TextFormatter<>((UnaryOperator<TextFormatter.Change>) change -> {
int a = change.getAnchor();
int b = change.getCaretPosition();
String newText = change.getControlNewText();
String prefix = newText.substring(0, Math.min(a, b));
String suffix = newText.substring(Math.max(a, b));
listView.getItems().clear();
for (String mid : replacements) {
listView.getItems().add(prefix + mid + suffix);
}
// do not change anything about the modification
return change;
});
textField.setTextFormatter(formatter);
Scene scene = new Scene(new VBox(listView, textField));
primaryStage.setScene(scene);
primaryStage.show();
}
还有很多可用的属性,包括更改前的文本和插入符以及锚点位置,请参阅 the javadoc。
我正在 Javafx 中实现自动完成逻辑。 TextField
用于用户输入,每次更新时,都会触发侦听器。然后一个 listView
的下拉菜单将显示匹配的结果。
所以问题是,当 textProperty
侦听器正在处理时,我如何才能在 TextField
中获取更新的插入符号位置?
我目前正在使用 textfield.getCaretPosition()
,但它只会 return 之前的位置。
如有任何意见,我们将不胜感激!
commandTextField.textProperty().addListener(new ChangeListener<String>() {
@Override
public void changed(ObservableValue<? extends String> observable,
String oldText, String newText) {
// Following line will return previous caret position
commandTextField.getCaretPosition();
...//autocomplete logic omitted
}
});
编辑: 我需要获取插入符位置的原因是我需要知道输入更改的位置并相应地显示建议。
textField.caretPositionProperty().addListener((ChangeListener<Number>) (observable, oldValue, newValue) -> {
caretPosition = newValue.intValue();
});
textField.textProperty().addListener((ChangeListener<String>) (observable, oldValue, newValue) -> {
...//autocomplete Logic
}
在上述情况下,textProperty
侦听器将首先被调用,然后是 caretPositionProperty
,因此在我的情况下无法获得更新的插入符号位置。
向 caretPositionProperty 添加侦听器。
import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.scene.Scene;
import javafx.scene.control.TextField;
import javafx.stage.Stage;
public class TextFieldCaretApp extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage stage) throws Exception {
TextField textField = new TextField();
Scene scene = new Scene(textField);
stage.setScene(scene);
stage.show();
textField.caretPositionProperty().addListener((ChangeListener<Number>) (observable, oldValue, newValue) -> {
System.out.println("caretPosition=" + newValue);
});
textField.textProperty().addListener((ChangeListener<String>) (observable, oldValue, newValue) -> {
System.out.println("text=" + newValue);
});
}
}
这不是 API 的预期用途,但您可以使用以 UnaryOperator<TextFormatter.Change>
作为参数构造的 TextFormatter
。这通常用于 prevent/modify 对文本的意外更改,但在这种情况下,我们将仅使用 Change
对象来获取您想要的数据。
以下示例 "suggests" 种可能性,每种可能性都用某个字符串替换所选部分:
@Override
public void start(Stage primaryStage) throws Exception {
ListView<String> listView = new ListView<>();
String[] replacements = { "foo", "bar", "42", "zap" };
listView.getItems().setAll(replacements);
TextField textField = new TextField();
TextFormatter<?> formatter = new TextFormatter<>((UnaryOperator<TextFormatter.Change>) change -> {
int a = change.getAnchor();
int b = change.getCaretPosition();
String newText = change.getControlNewText();
String prefix = newText.substring(0, Math.min(a, b));
String suffix = newText.substring(Math.max(a, b));
listView.getItems().clear();
for (String mid : replacements) {
listView.getItems().add(prefix + mid + suffix);
}
// do not change anything about the modification
return change;
});
textField.setTextFormatter(formatter);
Scene scene = new Scene(new VBox(listView, textField));
primaryStage.setScene(scene);
primaryStage.show();
}
还有很多可用的属性,包括更改前的文本和插入符以及锚点位置,请参阅 the javadoc。