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

TableView.getSelectionModel().getSelectedItems() returns null when rows are selected in a specific order.

    XMLWordPrintable

    Details

    • Subcomponent:
    • Introduced In Version:
    • CPU:
      x86
    • OS:
      windows_7, windows_8

      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();

        Attachments

          Issue Links

            Activity

              People

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

                Dates

                Created:
                Updated:
                Resolved: