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

[ListView] getSelectedIndices: list from Change has unexpected side effects (again)

XMLWordPrintable

    • generic
    • generic

      FULL PRODUCT VERSION :
      java version "1.8.0_121"
      Java(TM) SE Runtime Environment (build 1.8.0_121-b13)
      Java HotSpot(TM) 64-Bit Server VM (build 25.121-b13, mixed mode)

      ADDITIONAL OS VERSION INFORMATION :
      Microsoft Windows [Version 10.0.14393]

      A DESCRIPTION OF THE PROBLEM :
      I'm using a ListView with getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE) and have added a ListChangeListener to getSelectionModel().getSelectedIndices(). The listener receives the Change c and prints the changed list twice with System.out.println(c.getList().toString() + " " + c.getList().toString()). Both strings should be equal, but in some cases they're not, for example [-1] [2]. The first string is wrong in this cases.

      I created a similiar ticket last year (JDK-8147820).

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      1. press and hold ctrl
      2. select 1st row with a mouse click
      3. select 3rd row with a mouse click
      4. deselect 1st row with a mouse click


      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      The printed strings should be equal.
      ACTUAL -
      The printed strings are unequal.

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      import javafx.application.Application;
      import javafx.collections.FXCollections;
      import javafx.collections.ListChangeListener;
      import javafx.collections.ObservableList;
      import javafx.scene.Scene;
      import javafx.scene.control.ListView;
      import javafx.scene.control.SelectionMode;
      import javafx.scene.layout.VBox;
      import javafx.stage.Stage;

      public class ListViewExample extends Application {

          private void init(Stage primaryStage) {
              VBox root = new VBox(2);
              primaryStage.setScene(new Scene(root, 300, 300));

              final ObservableList<String> data = FXCollections.observableArrayList(
                      "Jacob Smith",
                      "Isabella Johnson",
                      "Ethan Williams");


              ListView listView = new ListView();
              listView.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE);
              listView.setItems(data);

              listView.getSelectionModel().getSelectedIndices().addListener(
                      (ListChangeListener) c -> System.out.println(c.getList().toString() + " " + c.getList().toString())
              );

              root.getChildren().addAll(listView);
          }

          @Override
          public void start(Stage primaryStage) throws Exception {
              init(primaryStage);
              primaryStage.setTitle("List Test " + System.getProperty("javafx.runtime.version"));
              primaryStage.show();
          }

          public static void main(String[] args) {
              launch(args);
          }
      }
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      Just iterate over the list before using it.

            scfitch Stephen Fitch
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            6 Start watching this issue

              Created:
              Updated:
              Resolved: