JDK-8159795 : TableView.getSelectionModel().getSelectedItems() returns null when rows are selected in a specific order.
  • Type: Bug
  • Component: javafx
  • Sub-Component: controls
  • Affected Version: 8u92
  • Priority: P3
  • Status: Closed
  • Resolution: Duplicate
  • OS: windows_7,windows_8
  • CPU: x86
  • Submitted: 2016-06-14
  • Updated: 2016-06-27
  • Resolved: 2016-06-27
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 8
8-poolResolved
Related Reports
Duplicate :  
Description
FULL PRODUCT VERSION :
java version "1.8.0_92"
Java(TM) SE Runtime Environment (build 1.8.0_92-b14)
Java HotSpot(TM) 64-Bit Server VM (build 25.92-b14, mixed mode)

ADDITIONAL OS VERSION INFORMATION :
Microsoft Windows [Version 6.3.9600]

A DESCRIPTION OF THE PROBLEM :
In a TableView with SelectionMode.MULTIPLE as selection mode, getSelectionMolde().getSelectedItems() will return null if you selected two rows in a specific order and then select a third row (while clearing the old selection) below. 

The selection of the first two rows needs to be done downwards, and the selection of the third row needs done on a row below the two previously selected rows.

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
1. Select the first row with item "One".
2. Add the second row with item "Two" to the selection by pressing CTRL or SHIFT and clicking the row.
3. Right click on the third row with item "Three" without pressing CTRL or SHIFT, this will remove the previous selection and select this row.
4. Choose the "Print selected rows"  in the ContextMenu that appeared when right clicking on the third row. This will print the toString() of each selected row.

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
Output in the console should be the toString() of the third row with item "Three": [Three] 


ACTUAL -
Output in the console will be "null", even though the third row with item "Three" is selected.

REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
import javafx.application.Application;
import javafx.beans.property.SimpleStringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Scene;
import javafx.scene.control.ContextMenu;
import javafx.scene.control.MenuItem;
import javafx.scene.control.SelectionMode;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;

public class MCVE2 extends Application {
	@Override
	public void start(Stage stage) {
		TableView<ObservableList<String>> table = new TableView<ObservableList<String>>();
		table.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE);

		// Initializes a column and adds it to the table.
		TableColumn<ObservableList<String>, String> col = new TableColumn<ObservableList<String>, String>("Column");
		col.setCellValueFactory(param -> new SimpleStringProperty(param.getValue().get(0)));
		table.getColumns().add(col);

		// Add data to the table.
		table.getItems().add(FXCollections.observableArrayList("One"));
		table.getItems().add(FXCollections.observableArrayList("Two"));
		table.getItems().add(FXCollections.observableArrayList("Three"));
		table.getItems().add(FXCollections.observableArrayList("Four"));

		// Initializes a ContextMenu and adds a MenuItem to it.
		ContextMenu contextMenu = new ContextMenu();
		table.setContextMenu(contextMenu);
		MenuItem menuItem = new MenuItem("Print selected rows");
		// Add an EventHandler that will print the toString() of all selected
		// rows in the table.
		menuItem.setOnAction(e -> {
			// Retrieving an iterator from getSelectedItems() and invoking
			// next() on it will prevent the bug from happening.
			// table.getSelectionModel().getSelectedItems().iterator().next();

			for (ObservableList<String> item : table.getSelectionModel().getSelectedItems()) {
				System.out.println(item);
			}
		});
		contextMenu.getItems().add(menuItem);

		BorderPane view = new BorderPane();
		view.setCenter(table);

		stage.setScene(new Scene(view));
		stage.show();
	}

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

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

CUSTOMER SUBMITTED WORKAROUND :
Retrieving an iterator from getSelectionModel().getSelectedItems() and invoking next() on it will prevent the bug from happening:

table.getSelectionModel().getSelectedItems().iterator().next();


Comments
I tested this on my machine with 8u66, and can recreate the issue. However, when I test with 8u-dev, I am unable to recreate the issue. It appears that this was already resolved in 8u-dev due to a backport I pushed on the 16th May 2016 for JDK-8144501. Closing this issue as a duplicate of JDK-8144501.
27-06-2016

Checked this on windows 7 and could confirm the issue as reported by the submitter. Steps to reproduce: ************************* - Run the attached test case(Mcve2.java) with JDK. Result: ******* OS: Windows 7 64 bit JDK (JAVAC, Netbeans 8.1): ************************** 8 b132 : Pass 8u40 b26 : Pass 8u45 b14 : Pass 8u51 b16 : Pass 8u52ea b07 : Pass => 8u60 b27 : Fail <= 8u66 b18 : Fail 8u92 b31 : Fail 8u102ea b05 : Fail 9ea+122 : Pass Introduced Version: JDK 8u60 ======================================================================================================================== Since it is a regression one and the introduced version is 8u60, moving this issue to JDK for further dev team's assessment.
17-06-2016