JDK-8160973 : [ListView] getSelectedItems() returns null element.
  • Type: Bug
  • Component: javafx
  • Sub-Component: controls
  • Affected Version: 8u112
  • Priority: P4
  • Status: Closed
  • Resolution: Won't Fix
  • OS: windows_7
  • CPU: x86_64
  • Submitted: 2016-07-07
  • Updated: 2016-07-07
  • Resolved: 2016-07-07
Related Reports
Relates :  
Description
Run this SSCCE:

"

import javafx.application.Application;
import static javafx.application.Application.launch;
import javafx.beans.InvalidationListener;
import javafx.beans.Observable;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Scene;
import javafx.scene.control.ListView;
import javafx.scene.control.SelectionMode;
import javafx.stage.Stage;

public class ListViewTest extends Application {

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

    ObservableList<String> list = FXCollections.observableArrayList("Kevin Rushforth", "Jonathan Giles");

    @Override
    public void start(Stage primaryStage) throws Exception {

        ListView listView1 = new ListView(list);
        listView1.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE);

        for (String string : list) {
            listView1.getSelectionModel().select(string);
        }
        primaryStage.setScene(new Scene(listView1));
        primaryStage.setWidth(800);
        primaryStage.setHeight(600);
        primaryStage.show();

        listView1.getSelectionModel().getSelectedItems().addListener(new InvalidationListener() {
            @Override
            public void invalidated(Observable observable) {
                System.out.println(listView1.getSelectionModel().getSelectedItems());
                System.out.println(listView1.getSelectionModel().getSelectedIndex());
                System.out.println(listView1.getSelectionModel().getSelectedItem());
                System.out.println(listView1.getSelectionModel().getSelectedIndices());
            }
        });
    }
}

"

Simply click on the second item "Jonathan Giles", then you see in the console:
"[null]
1
Jonathan Giles
[1]"

As you can see, the selection model has a null element in the getSelectedItems() list.

Tested with 8u112 Build b01

Comments
This is fixed in 9-dev, but I'm very reluctant to try to find a fix for this in 8u-dev, because it will possibly be complex, but also I'm not sure there is much of a window to get fixes into 8u-dev. Because you've published a workaround, I'm going to close this as "won't fix".
07-07-2016

Work-around : protected void verifySelectedItems(MultipleSelectionModel<T> selectionModel) { boolean problem = false; for (T item : selectionModel.getSelectedItems()) { if (item == null) { problem = true; } } if (problem) { List<Integer> selectedIndices = new ArrayList<>(selectionModel.getSelectedIndices()); for (int indice : selectedIndices) { selectionModel.select(indice); } } }
07-07-2016