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

JavaFX ListView ListChangeListener.Change wrong for SelectionMode.MULTIPLE

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Resolved
    • Priority: P4
    • Resolution: Cannot Reproduce
    • Affects Version/s: 8u5
    • Fix Version/s: 8u20
    • Component/s: javafx
    • Environment:

      Mac OS X 10.9.2, jdk1.8.0_05

      Description

      The default selection model of JavaFX ListView seems to report wrong values to the listener in the Change object, if the selection mode is SelectionMode.MULTIPLE. (If my interpretation of Change is correct.)

      I have a list view with items "Julia", "Ian", "Sue", etc. The code below generates the following output:

      Clicking first item "Julia" ("Julia" is selected, output correct):

      from 0, to 1, removed 0, added 1
      was added
      removed:
      added:
          Julia
      selected:
          Julia

      Shift + ArrowDown ("Julia" and "Ian" are selected):

      from 0, to 1, removed 0, added 1 <-- should be from 1, to 2
      was added
      removed:
      added:
          Julia <-- should be Ian
      selected:
          Julia
          Ian

      Shift + ArrowDown ("Julia", "Ian", and "Sue" are selected):

      from 0, to 1, removed 0, added 1 <-- should be from 2, to 3
      was added
      removed:
      added:
          Julia <-- should be Sue
      selected:
          Julia
          Ian
          Sue



      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.MultipleSelectionModel;
      import javafx.scene.control.SelectionMode;
      import javafx.stage.Stage;

      public class rt36941 extends Application {

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

          public void start(Stage stage) {
              ObservableList<String> items = FXCollections.observableArrayList(
                      "Julia", "Ian", "Sue", "Matthew", "Hannah", "Stephan", "Denise");
              ListView<String> listView = new ListView<>(items);

              MultipleSelectionModel<String> selectionModel = listView.getSelectionModel();
              selectionModel.setSelectionMode(SelectionMode.MULTIPLE);
              ObservableList<String> selectedItems = selectionModel.getSelectedItems();

              selectedItems.addListener((ListChangeListener.Change<? extends String> c) -> {
                  while (c.next()) {
                      System.out.printf("from %d, to %d, removed %d, added %d\n",
                              c.getFrom(), c.getTo(), c.getRemovedSize(), c.getAddedSize());
                      if (c.wasAdded()) System.out.println("was added");
                      if (c.wasRemoved()) System.out.println("was removed");
                      if (c.wasPermutated()) System.out.println("was permutated");
                      if (c.wasUpdated()) System.out.println("was updated");
                      if (c.wasReplaced()) System.out.println("was replaced");
                      System.out.println("removed:");
                      for (String e : c.getRemoved()) {
                          System.out.println(" " + e);
                      }
                      System.out.println("added:");
                      for (String e : c.getAddedSubList()) {
                          System.out.println(" " + e);
                      }
                      System.out.println("selected:");
                      for (String e : selectedItems) {
                          System.out.println(" " + e);
                      }
                      System.out.println();
                  }
              });

              Scene scene = new Scene(listView);

              stage.setScene(scene);
              stage.show();
          }
      }

        Attachments

          Activity

            People

            Assignee:
            jgiles Jonathan Giles
            Reporter:
            duke J. Duke (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Dates

              Created:
              Updated:
              Resolved:
              Imported: