<AWT Dev> RFR: 6278172: TextComponent.getSelectedText() throws StringIndexOutOfBoundsException [v2]

Sergey Bylokhov serb at openjdk.java.net
Fri Jan 8 00:34:55 UTC 2021


On Thu, 7 Jan 2021 20:11:31 GMT, Alexey Ivanov <aivanov at openjdk.org> wrote:

> I wonder how native text components behave: is the selection preserved when text is replaced?

On windows, the selection resets, on macOS and Linux it is preserved. But it works that way because we implemented it in a such way, actually did not pay attention to such use-case.

> Do I get it right that if the new text is longer than the initial one, then only the portion will be left selected.

For the case:
 1. the user sets the short text
 2. then selects the longer distance
 3. then sets the longer text
nothing is changed. The select() method at step 2 will trim passed parameters based on the short text at step 1. So only the portion of the text will be selected.

> Won't it cause the issue if the scenario where the selectionStart is past the length of the new text. I mean the only "field" portion of "text field" is selected, and then the text in component is replaced with "text", thus the selectionStart points 1 character past the end of the new text.

The new call to select() method should take care of that since the user as well can call it passing wrong parameters.

-------------

PR: https://git.openjdk.java.net/jdk/pull/1104


More information about the awt-dev mailing list