Search code examples
javafxcomboboxcaret

Javafx ComboBox set Caret Position


When I press a key in an editable ComboBox, caret is always at pos 0 and not at pos 1 as expected. Whatever text I type, caret position should be after the last character entered into editable combobox. Instead of it, caret position is always positioned at pos 0. Any idea how to position the caret correctly after entering the text?

In java Swing, I used to achieve the goal as follow

((JTextField)jComboBox.getEditor().getEditorComponent()).setCaretPosition(enteredString.length());

but editor of ComboBox from JavaFX does not have method getEditorComponent(). Even TextField from JavaFX does not have method getCaretPosition();

Any idea how to position the caret correctly after entering the text in Editable ComboBox?


Solution

  • //To get the position
    int origCarrotPos = comboBox.getEditor().getCaretPosition();
    
    //To set the position
    int carrotPos = 5;
    comboBoxgetEditor().positionCaret(carrotPos);