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

SelectionModel.select(0) doesn't update SelectionModel.getSelectedItem() after deletion of underlying item

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Cannot Reproduce
    • Icon: P4 P4
    • 8
    • fx2.1
    • javafx
    • Mac OS X 10.7.3

      java version "1.6.0_29"
      Java(TM) SE Runtime Environment (build 1.6.0_29-b11-402-11D50b)
      Java HotSpot(TM) 64-Bit Server VM (build 20.4-b02-402, mixed mode)

      iMac Mid 2011

      I attach a short program which illustrates a phenomenon I have observed. Basically, I am trying to repeatedly delete the first item displayed in a ListView. I select that item using SelectionModel.select(0) or SelectionModel.selectFirst() and then delete SelectionModel.getSelectedItem() from the underlying item list. The first time round it works. The second time round, SelectionModel.getSelectedItem() still returns the item from the first iteration. I would have expected the call to SelectionModel.select(0) to update selectedItemProperty.

      Interestingly, a similar thing occurs if you replace select(0) with selectLast().

      But if you call select(1) everything works as I had expected (selectedItemProperty is updated).

      After discussion in the forum it would appear that this behaviour is consistent with the JavaDoc, but I have posted this issue at the request of Jonathan Giles.


      package selectionfeature;

      import javafx.application.Application;
      import javafx.beans.value.ChangeListener;
      import javafx.beans.value.ObservableValue;
      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.control.MultipleSelectionModel;
      import javafx.scene.layout.VBox;
      import javafx.stage.Stage;


      /**
       * DOCUMENT ME!
       *
       * @author $author$
       * @version $Revision$, $Date$
       */
      public class SelectionFeature extends Application {

          /**
           * Creates a new SelectionFeature object.
           */
          public SelectionFeature() { }

          /**
           * DOCUMENT ME!
           *
           * @param args DOCUMENT ME!
           */
          public static void main(String[] args) {
              Application.launch(SelectionFeature.class, args);
          }

          /**
           * DOCUMENT ME!
           *
           * @param primaryStage DOCUMENT ME!
           */
          @Override
          public void start(Stage primaryStage) {

              final ListView<String> list = new ListView<String>();
              ObservableList<String> items =
                  FXCollections.observableArrayList("abc", "def", "ghi", "jkl", "mno", "pqr", "stu", "vwx");
              list.setItems(items);
              list.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<String>() {

                  @Override
                  public void changed(ObservableValue<? extends String> ov, String oldValue, String newValue) {
                      System.out.println("*** ChangeListener: "+oldValue+" -> "+newValue);
                  }
              });

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

                      @Override
                      public void handle(ActionEvent t) {
                          final MultipleSelectionModel<String> selectionModel = list.getSelectionModel();
                          selectionModel.select(0); // oops
                          //selectionModel.select(1); // OK
                          //selectionModel.selectFirst(); // oops
                          //selectionModel.selectLast(); // oops

                          String string = selectionModel.getSelectedItem();
                          System.out.println(string);

                          if (list.getItems().remove(string)) {
                              System.out.println("removed");
                          } else {
                              System.out.println("oops");
                          }
                          //selectionModel.clearSelection(); // work-around
                      }
                  });

              VBox vBox = new VBox();
              vBox.getChildren().addAll(list, button);

              Scene scene = new Scene(vBox);

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

            jgiles Jonathan Giles
            swinnalljfx Stephen Winnall (Inactive)
            Votes:
            1 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported: