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

[ListView] Scroll position in ListView is not correctly updated when adding new items

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Unresolved
    • Icon: P4 P4
    • tbd
    • 8u5
    • javafx
    • None

      When inserting items in a ListView (or TableView), the scroll position is kept relative to the whole content. If the scroll position is at 50% and we double the number of items, the position is still at 50% thus showing a completely different section. I would expect that the current view section does not change, otherwise the list is not usable anymore while adding items concurrently.

      Here's some demo code:

      import java.util.Timer;
      import java.util.TimerTask;

      import javafx.application.Application;
      import javafx.application.Platform;
      import javafx.scene.Scene;
      import javafx.scene.control.ListView;
      import javafx.scene.layout.VBox;
      import javafx.stage.Stage;

      public class ListScrollTest extends Application {
        @Override
        public void start(Stage primaryStage) throws Exception {
          Scene scene = new Scene(new VBox(), 400, 200);
          
          // a list containing 20 items
          ListView<String> listView = new ListView<>();
          for (int i = 0; i < 20; i++) {
            listView.getItems().add("item " + i);
          }
          listView.getSelectionModel().select(10);
          listView.scrollTo(10);
          
          // add new items at the bottom concurrently
          new Timer().schedule(new TimerTask() {
            @Override
            public void run() {
              Platform.runLater(new Runnable() {
                public void run() {
                  listView.getItems().add("make list unusable");
                }
              });
            }
          }, 2000, 200);
          
          ((VBox) scene.getRoot()).getChildren().addAll(listView);
          
          primaryStage.setScene(scene);
          primaryStage.show();
        }
        
        public static void main(String[] args) {
          launch(args);
        }
      }

            Unassigned Unassigned
            duke J. Duke
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Imported: