RFR: 8333275: ComboBox: adding an item from editor changes editor text

Andy Goryachev angorya at openjdk.org
Fri Jan 31 17:36:52 UTC 2025


On Fri, 31 Jan 2025 02:37:42 GMT, Michael Strauß <mstrauss at openjdk.org> wrote:

> When the current editor value of a `ComboBox` is added to its items list, the editor value may be changed to an entirely unrelated item in the list. However, our expectation would be that the editor reflects the newly added item instead.
> 
> Under normal circumstances, adding items to the list in front of the current item causes `selectedIndex` to be shifted by the number of items added, keeping the editor value in sync with the currently selected item.
> 
> Let's illustrate this with an example:
> `items = [foo, bar, baz]`, `selectedIndex = 0`, `selectedItem = foo`
> 
> The user changes the editor value to `qux` and inserts the value at list position 0, causing the current index to be shifted to the right (in `ComboBoxSelectionModel::itemsContentObserver`):
> `items = [qux, foo, bar, baz]`, `selectedIndex = 1`, `selectedItem = foo`
> 
> The index is shifted a second time in `ListViewBitSetSelectionModel::updateSelection`:
> `items = [qux, foo, bar, baz]`, `selectedIndex = 2`, `selectedItem = bar`
> 
> Now `bar` is selected and shown in the editor, but we would expect `qux` instead. The fix is to detect that we're inserting the current editor value in `ComboBoxSelectionModel::itemsContentObserver`, and select the correct item directly. We also need to account for this situation in `ListViewBitSetSelectionModel`, where we check whether the current value is already contained in the added values.

modules/javafx.controls/src/main/java/javafx/scene/control/ComboBox.java line 601:

> 599:                 }
> 600: 
> 601:                 int index = -1;

you may want to change the copyright year to 2025 in all the modified files

modules/javafx.controls/src/main/java/javafx/scene/control/ListView.java line 1315:

> 1313:                             && (addedItemOffset = c.getAddedSubList().indexOf(selectedItem)) >= 0
> 1314:                             && selectedIndex == c.getFrom() + addedItemOffset) {
> 1315:                         doSelectionUpdate = false;

which scenario does this code path handle outside of tests?

I could never hit a breakpoint in line 1315, except in ensureEditorValueDoesNotChangeWhenCurrentEditorValueIsAddedToItemsList().

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

PR Review Comment: https://git.openjdk.org/jfx/pull/1692#discussion_r1937647640
PR Review Comment: https://git.openjdk.org/jfx/pull/1692#discussion_r1937646638


More information about the openjfx-dev mailing list