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

ListView multi-selection event reports wrong index in special case

XMLWordPrintable

    • x86
    • other

      FULL PRODUCT VERSION :
      java version 1.8.0_92
      Java SE Runtime Environment, build 1.8.0_92-b14
      Java Hotspot 64-Bit Server VM (build 25.92-b14, mixed mode)

      ADDITIONAL OS VERSION INFORMATION :
      Microsoft Windows 10.0.10586

      EXTRA RELEVANT SYSTEM CONFIGURATION :
      nothing special, 8gb ram, quad-core

      A DESCRIPTION OF THE PROBLEM :
      Selecting the first three items in the order 0-2-1 of a list view in multiple selection mode fires add-events with the items 0-2-0 instead of 0-2-1 but the selection list itself contains the correct elements 0-2-1 - see example code.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      just start the attached sample. the programm selects the first three items in the order 0-2-1 and the listener prints the following items of the was-added event: 0-2-0 instead of 0-2-1. this happens even if the items are selected manually using the gui.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      items 0-2-1
      ACTUAL -
      items 0-2-0

      ERROR MESSAGES/STACK TRACES THAT OCCUR :
      none, just a wrong behaviour

      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.StackPane;
      import javafx.stage.Stage;
       
      /**
       *
       * based on example from
       * http://docs.oracle.com/javafx/2/ui_controls/list-view.htm
       *
       */
      public class ListViewSelection extends Application
      implements ListChangeListener<String> {
           

          public static void main(String[] args) {
              launch(args);
          }
          
          
          @Override
          public void start(Stage primaryStage) {
              primaryStage.setTitle("ListView");
             
              System.err.println(System.getProperty("java.vendor")+" "+System.getProperty("java.version"));
              System.err.println(System.getProperty("os.name")+" "+System.getProperty("os.arch")+" "+System.getProperty("os.version"));
              
              ListView<String> listView = new ListView<>();
              listView.setPrefSize(200, 250);
              listView.setEditable(true);
              listView.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE);
              
              ObservableList<String> items = FXCollections.observableArrayList();
              for( int i = 0; i < 600; i++ ) {
               items.add("v"+i);
              }
              listView.setItems(items);
              listView.getSelectionModel().getSelectedItems().addListener(this);
                   
              StackPane root = new StackPane();
              root.getChildren().add(listView);
              primaryStage.setScene(new Scene(root, 200, 250));
              primaryStage.show();
              
              // select some items...
              // the result is the same when selecting via gui
              // but only on the _first_ three items
              
           listView.getSelectionModel().select(0);
           listView.getSelectionModel().select(2);
           listView.getSelectionModel().select(1);
          
           // result:
           //
           // v0
           // v2
           // v0
           //
           // expected result:
           //
           // v0
           // v2
           // v1 (???)
           //
          
          }

      /**
      * selection listener
      */
      @Override
      public void onChanged(ListChangeListener.Change<? extends String> r) {
      while( r.next() ) {

      assert !(r.wasAdded() && r.wasRemoved()); // happens in my application on further selections

      if( r.wasAdded() ) {
      for(int i = r.getFrom(); i < r.getTo(); i++) {

      String e = r.getList().get(i);
      System.out.println(e);
      }
      }
      else {
      System.out.println("unhandled event "+r);
      }

      } // while

      // done
      }

      }

      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      Iterate the list of the selected items instead of using the index range of the wasAdded event - but that is a lot of work within an application.

            jgiles Jonathan Giles
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated:
              Resolved: