JDK-8158895 : ListView multi-selection event reports wrong index in special case
  • Type: Bug
  • Component: javafx
  • Sub-Component: controls
  • Affected Version: 8u92
  • Priority: P3
  • Status: Closed
  • Resolution: Won't Fix
  • OS: other
  • CPU: x86
  • Submitted: 2016-06-05
  • Updated: 2016-06-27
  • Resolved: 2016-06-27
Related Reports
Relates :  
Relates :  
Description
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.


Comments
I have investigated this issue and can reproduce it in 8u-dev. I can not reproduce it in 9-dev, as it has been fixed as part of the large overhaul that the selection model code received earlier in the 9-dev development cycle. It would not be trivial or safe to backport all changes made in 9-dev into 8u-dev: the chance for regression is too high. Alternatively, crafting an 8u-dev specific fix without the benefit of all other fixes in 9-dev is likely to be difficult and also prone to cause regressions. I would therefore prefer to close this issue as won't fix, to ensure that 8u-dev is not further destabilised for fixes that are, by the submitters own admission, a special case.
27-06-2016

This is likely a duplicate of one or more other bugs already fixed in JDK 9, since we have fixed several bugs in this area.
07-06-2016

Checked this for 8u0,8u92,9ea+121 on Windows 7 and Could confirm the issue as submitter reported. Steps to reproduce: ================ Run the Attached test case(ListViewSelection.java) with the JDK version. ------------------------------------------------------------------------------------------------------------- Test Result: --------------- OS : Windows 7 64 bit JDK: 7u80 b15 : Fail [v0,v0,v0] 8u0 b132 : Pass [v0,v2,v1] 8u40 b26 : Pass [v0,v2,v1] 8u45 b14 : Pass [v0,v2,v1] 8u51 b16 : Pass [v0,v2,v1] 8u52ea b07 : Pass [v0,v2,v1] 8u60 b27 : Fail [V0,v2,v0] 8u66 b18 : Fail [V0,v2,v0] 8u74 b02 : Fail [V0,v2,v0] 8u92 b31 : Fail [v0,v2,v0] 9ea+121 : Pass [v0,v2,v1] O/P: 9ea+121: ****************** >javac ListViewSelection.java >java ListViewSelection Oracle Corporation 9-ea Windows 7 amd64 6.1 v0 v2 v1 O/P (8u60 b27): ********************* >javac ListViewSelection.java >java ListViewSelection Oracle Corporation 1.8.0_60 Windows 7 amd64 6.1 v0 v2 v0 --------------------------------------------------------------------------------------------------------------- It looks like a genuine bug to me. Therefore, moving this issue over JDK for dev team's evaluation.
07-06-2016