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

ListView Multiselection: selected items list change during two iterations

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Unresolved
    • Icon: P4 P4
    • tbd
    • 8u65
    • javafx
    • x86_64
    • windows_7

      FULL PRODUCT VERSION :
      java version "1.8.0_65"
      Java(TM) SE Runtime Environment (build 1.8.0_65-b17)
      Java HotSpot(TM) 64-Bit Server VM (build 25.65-b01, mixed mode)

      ADDITIONAL OS VERSION INFORMATION :
      Microsoft Windows [Version 6.1.7601]

      A DESCRIPTION OF THE PROBLEM :
      ListView.getSelectionModel().getSelectedItems and ListView.getSelectionModel.getSelectedIndices return lists that are not stable during iteration. see steps to reproduce and the test case.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      1. create a JavaFX ListView and populate it with a ObservableList containing two items.
      2. enable multi selection.
      3. Select the first item. The first item is now selected.
      4. Select the second item. Both items are now selected.
      5. Deselect the first item.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      Iterating multiple times over the same selectedItems List should return the same list. A list containing second item of the original list.
      ACTUAL -
      The first iteration returns one item being null.

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      import javafx.collections.FXCollections;
      import javafx.collections.ObservableList;
      import javafx.embed.swing.JFXPanel;
      import javafx.scene.control.ListView;
      import javafx.scene.control.SelectionMode;

      import javax.swing.*;
      import java.util.ArrayList;
      import java.util.concurrent.CountDownLatch;

      public class ListViewSelectionBug {

        public static void main(String[] args) throws InterruptedException {

          final CountDownLatch latch = new CountDownLatch(1);
          SwingUtilities.invokeLater(() -> {
            new JFXPanel(); // initializes JavaFX environment
            latch.countDown();
          });
          latch.await();
          ObservableList<String> list = FXCollections.observableArrayList("first", "second");

          ListView<String> listView = new ListView<>(list);
          listView.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE);
          listView.getSelectionModel().select(0);
          System.out.println(listView.getSelectionModel().getSelectedItems());
          listView.getSelectionModel().select(1);
          System.out.println(listView.getSelectionModel().getSelectedItems());
          listView.getSelectionModel().clearSelection(0);

          ArrayList firstCopy = new ArrayList<>(listView.getSelectionModel().getSelectedItems());
          ArrayList secondCopy = new ArrayList<>(listView.getSelectionModel().getSelectedItems());
          System.out.println("are both copies equal? " + firstCopy.equals(secondCopy));
          System.out.println("first copy: " + firstCopy);
          System.out.println("second copy: " + secondCopy);

        }
      }
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      Iterating over the selected items before evaluating them.

            Unassigned Unassigned
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated: