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

Unexpected interaction between ListView's SelectionModel getSelecteditems() and removing items

    XMLWordPrintable

    Details

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

      Java 8.0 b 129

      Description

      import java.util.ArrayList;
      import java.util.List;
      import javafx.application.Application;
      import javafx.collections.FXCollections;
      import javafx.collections.ObservableList;
      import javafx.event.ActionEvent;
      import javafx.event.EventHandler;
      import javafx.scene.Scene;
      import javafx.scene.control.Button;
      import javafx.scene.control.ListView;
      import javafx.scene.layout.VBox;
      import javafx.stage.Stage;

      public class Bug extends Application {

          public static void main(String[] args) {
              launch(args);
          }
          
          @Override public void start(Stage stage) {
              ObservableList<String> fxList = FXCollections.observableArrayList("A", "B", "C");
              final ListView<String> listView = new ListView<String>(fxList);

              Button button = new Button("Remove");
              button.setOnAction(new EventHandler<ActionEvent>() {

                  @Override
                  public void handle(ActionEvent t) {
                      ObservableList<String> selectedItems = listView.getSelectionModel().getSelectedItems();
                      System.out.println("selectedItems = " + selectedItems);
                      listView.getItems().removeAll(selectedItems);
                  }
              });
              
              VBox vBox = new VBox();
              vBox.getChildren().setAll(listView, button);
              
              stage.setScene(new Scene(vBox));
              stage.show();
          }
      }


      Try it on a list, but only select the first item.

      Instead of just the first item being removed, the entire list is removed. It basically removes all items past the first selected item.

      Possibly related to RT-24371

        Attachments

        1. rt-35857_2.patch
          2 kB
        2. rt35857.patch
          7 kB
        3. rt35857-final.patch
          8 kB

          Issue Links

            Activity

              People

              Assignee:
              jgiles Jonathan Giles
              Reporter:
              swpalmer Scott Palmer
              Votes:
              0 Vote for this issue
              Watchers:
              3 Start watching this issue

                Dates

                Created:
                Updated:
                Resolved:
                Imported: