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

[ListView] ListView: selection/focus state broken after insert item

    XMLWordPrintable

    Details

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

      current 8u40b9, but probably happens in all versions after the 15793

      Description

      Below is an example of incorrect selection update on inserting items. Follow the instructions to reproduce. Note that it happens only if the items are set after the the list is instantiated.

      This is yet another variant of problems produced by the hacky solution of RT-15793: the real smell is indecent intimacy - we _must not_ interfere with class internals. The technical reason is that the contentsListeners of selection/focusModel are registered twice, thus shifting selection/focus two times instead of once.

      [removed: assumed quick solution - was wrong on that part, see comment]

      import javafx.application.Application;
      import javafx.collections.FXCollections;
      import javafx.collections.ObservableList;
      import javafx.scene.Parent;
      import javafx.scene.Scene;
      import javafx.scene.control.Button;
      import javafx.scene.control.ListView;
      import javafx.scene.layout.HBox;
      import javafx.stage.Stage;

      /**
       * Run and press button to insert item at selection
       * - select first item
       * - press button to insert item at selection
       * - expected: selection sticks to old selectedItem, that is now at
       * second position
       * - actual: selection at third
       *
       * @author Jeanette Winzenburg, Berlin
       */
      @SuppressWarnings({ "rawtypes", "unchecked" })
      public class ListViewInsert extends Application {

          int count;
          @Override
          public void start(Stage primaryStage) throws Exception {
              ObservableList items = FXCollections.observableArrayList("one", "two", "three");
              ListView listView = new ListView<>();
              listView.setItems(items);
              Button add = new Button("Insert at selection");
              add.setOnAction(e -> {
                  if (listView.getSelectionModel().getSelectedIndex() < 0) return;
                  items.add(listView.getSelectionModel().getSelectedIndex(), "item " + count++);
              });
              Parent pane = new HBox(listView, add);
              primaryStage.setScene(new Scene(pane));
              primaryStage.setTitle(System.getProperty("java.version"));
              primaryStage.show();
          }

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

        Attachments

          Issue Links

            Activity

              People

              Assignee:
              jgiles Jonathan Giles
              Reporter:
              fastegal Jeanette Winzenburg
              Votes:
              0 Vote for this issue
              Watchers:
              6 Start watching this issue

                Dates

                Created:
                Updated:
                Resolved:
                Imported: