You can try this trick to keep the cursor position in front setText()and then restore it as soon as you add text to the component:
int caretPosition = yourComponent.getCaretPosition();
yourComponent.setText(" your long text ");
yourComponent.setCaretPosition(Math.min(caretPosition, text.length()));
source
share