RFR: 8176270: Adding ChangeListener to TextField.selectedTextProperty causes StringOutOfBoundsException

Jeanette Winzenburg fastegal at openjdk.java.net
Thu Jun 11 14:41:27 UTC 2020

On Thu, 28 May 2020 06:50:57 GMT, Robert Lichtenberger <rlichten at openjdk.org> wrote:

>> Most of the time, value in javafx.scene.control.TextInputControl.replaceText(int, int, String, int, int) will already
>> be filtered (e.g. linebreaks in TextField) In that case, adjustmentAmount will be zero and one could just do the
>> selection before actually inserting the text.
>> But a case can be constructed with a TextFormatter that will produce characters that will be filtered by the TextField
>> itself:
>>     @Test public void replaceSelectionWithFilteredCharacters() {
>>         txtField.setText("x xxxyyy");
>>         txtField.selectRange(2, 5);
>>         txtField.setTextFormatter(new TextFormatter<>(this::noDigits));
>>         txtField.replaceSelection("a1234a");
>>         assertEquals("x aayyy", txtField.getText());
>>         assertEquals(4, txtField.getSelection().getStart());
>>         assertEquals(4, txtField.getSelection().getStart());
>>     }
>>     private Change noDigits(Change change) {
>>         Change filtered = change.clone();
>>         filtered.setText(change.getText().replaceAll("[0-9]","\n"));
>>         return filtered;
>>     }
> The last commit on this branch seems like the "best" way to fix this problem. It prevents in-between changes in
> selectedText and does not introduce (to the best of my knowledge) any new corner cases. It eliminiates the "root cause"
> of the problem by postponing selectedText update to the end of whole replace-operation. So, this is RFR now (I hope).

good direction, I think :)

Didn't look too closely, just added your changes and run the tests - getting a StringIndexOutofBounds at
TextInputControlTest.test_jdk_8171229_replaceText(TextInputControlTest.java:1862) (no failing test because the
uncaughtException handler not redirected). Could you check please?


PR: https://git.openjdk.java.net/jfx/pull/138

More information about the openjfx-dev mailing list