I need to set the caret position manually in my code.
There is a getCaretPosition()
under javafx.scene.control.TextInputControl
but there is no setter method.
How can I set the caret position?
I need to set the caret position manually in my code.
There is a getCaretPosition()
under javafx.scene.control.TextInputControl
but there is no setter method.
How can I set the caret position?
TextArea ta = new TextArea();
ta.setText("1234567890");
ta.positionCaret(4);
setCaretPosition
would be convenient –
Hydrogenate You can use the positionCaret function as mentioned before. But make sure to wrap it in Platform.runLater. Otherwise it might not work at all.
Platform.runLater( new Runnable() {
@Override
public void run() {
textArea.positionCaret( 0 );
}
});
There are two method in TextInputControl that allow manipulation of caret position. These are :-
selectPositionCaret(int pos) - Selects the text between the last caret position up to the current caret position that you entered.
positionCaret(int pos) - Sets the current caret position clearing the previous selection as well.
So i think in your case you want to use the positionCaret method to set the position without any selections.
if you want to add it at the end of your TextField you can do the next
TextFieldName.positionCaret(TextFieldName.getText().length());
this will add the Curser at The end .
© 2022 - 2024 — McMap. All rights reserved.