TextField - scroll to the current cursor position after setting the content


I wrote an Extension for a TextField that gives suggestions based on the current input and cursor position. After selecting a suggestion, I insert it into the TextField on client side and set the cursor position to after the inserted suggestion.

A problem occurs when the text content is longer than the TextField: The text is still shown from the beginning, even if the cursor is somewhere else. Only after I enter text or navigate the cursor to another position, the content is scrolled to the current cursor position.

Is there a way to force the widget to scroll to the current cursor position?

this helps
. Well, that’s for JavaScript, but GWT TextBox also has setSelectionRange(). Apparently, setting a range may help in keeping the cursor position visible. Not sure if that works.


Would you be interested in sharing your solution? I am looking for the same functionality and it would be much appreciated.


Thanks, but I’m already using set cursorPosition(cursorPos) for my purposes, which internally calls setSelectionRange(cursorPos, 0), so that doesn’t help. I noticed however that the textfield automatically scrolls to the cursor if I interact with the popup in another way (e.g. clicking a button to go to the next page of the suggestion list, which doesn’t result in a text change in the field). But when I change the text through an interaction with the popup, the textfield just scrolls to the beginning.

I basically wrote a suggestion popup similar to the one of combobox, and my extension requests and receives suggestions from the server through RPCs based on the current text and cursor position of the textfield widget. I can’t go into more detail though.

Hi, I experimented how setting the cursor position through the server-side API works and I didn’t have much problems (with Firefox). See the
example here
. The cursor itself often seems to hide when it’s at the end of the text field, also when moving the cursor with keyboard. That could be a theme problem, I’m not sure, but I’ll add a ticket about that.

Hi Marko,

Thanks for your answer. I’ve tried my client-side approach with Firefox and it seems to have a similar behavior to the one you describe - the textfield is scrolled to an appropriate position, but the field loses focus - however, Chrome and IE behave like I described it earlier.
I’ll try the server-side approach when I get around to it.