Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8267951

MultipleSelectionModelBase suppresses change notifications for selectedIndices

XMLWordPrintable

      In many places, MultipleSelectionModelBase suppresses change notifications for its `selectedIndices` list. One such example (among many others) is in line 405:

          if (getSelectionMode() == SINGLE) {
              startAtomic();
              quietClearSelection();
              stopAtomic();
          }
          selectedIndices.set(row);

      As a result, any listeners on `selectedIndices` will miss notifications, and will not be able to correctly reproduce the changes.

      For example, run this piece of code:

          ListView<String> listView = new ListView<>(FXCollections.observableArrayList("foo", "bar"));
          MultipleSelectionModel<String> model = listView.getSelectionModel();
          model.setSelectionMode(SelectionMode.SINGLE);
          model.getSelectedIndices().addListener((ListChangeListener<? super Integer>)System.out::println);
          model.select(0);
          model.select(1);

      You would expect the following output:

          { [0] added at 0 }
          { [0] replaced by [1] at 0 }

      However, the actual output is:

          { [0] added at 0 }
          { [1] added at 0 }

      The same problem occurs when subscribing to `selectedItems` instead of `selectedIndices`. Since selected items are updated whenever selected indices change, the `selectedItems` list will also miss some change notifications.

            aghaisas Ajit Ghaisas
            mstrauss Michael Strauß
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated: