I'm implementing the AutoComplete logic in Javafx. A TextField
is for user input and every time it got updated, the listener will be triggered. And then a dropdown menu which is a listView
will display the matched results.
So the problem is that how can I get the updated caret position in TextField
when the textProperty
listener is in process?
I'm currently using textfield.getCaretPosition()
, but it will only return the previous position.
Any inputs will be appreciated!
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
}
});
EDIT: The reason that I need to get the caret position is that I need to know the position where the input is changed and show suggestions correspondingly.
textField.caretPositionProperty().addListener((ChangeListener<Number>) (observable, oldValue, newValue) -> {
caretPosition = newValue.intValue();
});
textField.textProperty().addListener((ChangeListener<String>) (observable, oldValue, newValue) -> {
...//autocomplete Logic
}
In the above case, the textProperty
listener will be invoked first and followed by caretPositionProperty
, therefore there is no way to get updated caret position in my case.