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

ListView's scroll bar does not appear

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: P3 P3
    • fx2.1
    • fx2.0
    • javafx
    • WinXP SP3 + JDK 1.6.0_25 + javafx_sdk-2_0-fcs-b47-windows-i586-15_sep_2011

      ListView's scroll bar does not appear if items are added to the ObserverableList(containing the ListView's items) one by one

      Below codes can be used to re-produce

      How to re-produce:

      1. Run the sample
      2. Click <Go> button, you will see items are added to the ListView one by one and
          soon will see the number of the added items exceeds the number of items that
          can be displayed in the viewport of the ListView, but no scroll bar appears


      -----------
      /*
       * To change this template, choose Tools | Templates
       * and open the template in the editor.
       */
      package largelistviewissue;

      import javafx.application.Application;
      import javafx.collections.FXCollections;
      import javafx.collections.ObservableList;
      import javafx.concurrent.Service;
      import javafx.concurrent.Task;
      import javafx.event.ActionEvent;
      import javafx.event.EventHandler;
      import javafx.scene.Group;
      import javafx.scene.Scene;
      import javafx.scene.control.Button;
      import javafx.scene.control.ListView;
      import javafx.scene.text.Font;
      import javafx.scene.text.Text;
      import javafx.stage.Stage;

      /**
       *
       * @author lianqi.li
       */
      public class LargeListViewIssue extends Application {
          java.util.List<java.lang.String> fontNames = Font.getFontNames();
          ObservableList<java.lang.String> listItems = FXCollections.observableArrayList();
          Service s = null;

          @Override public void start(Stage stage) {
              stage.setTitle("TableView Issue");
              final Group root = new Group();
              final Scene scene = new Scene(root, 500, 400);

              final ListView<String> listView = new ListView<String>();
              listView.setTranslateX(10);
              listView.setTranslateY(10);
              listView.setPrefHeight(240);
              final Text t = new Text(10, 280, "");
              
              final Button bGo = new Button("Go");
              bGo.setLayoutX(10);
              bGo.setLayoutY(300);
              bGo.setOnAction(new EventHandler<ActionEvent>() {
                  @Override
                  public void handle(ActionEvent event) {
                      listView.getItems().clear();

                      s = new Service() {
                          @Override protected Task createTask() {
                              return new Task() {
                                  @Override public Void call() throws Exception {
                                      for(int i = 0; i < fontNames.size(); i++) {
                                          try {
                                              Thread.sleep(1000);
                                              listItems.add(fontNames.get(i));
                                              t.setText("" + listView.getItems().size() + " font names added");
                                          } catch(java.lang.Exception e) {}
                                      }
                                      return null;
                                  }
                              };
                          }
                      };

                      listView.setItems(listItems);
                      t.setText("" + listView.getItems().size() + " font names added");

                      s.start();
                  }
              });

              final Button bCancel = new Button("Cancel");
              bCancel.setLayoutX(100);
              bCancel.setLayoutY(300);
              bCancel.setOnAction(new EventHandler<ActionEvent>() {
                  @Override
                  public void handle(ActionEvent event) {
                      s.cancel();
                  }
              });

              root.getChildren().addAll(listView, t, bGo, bCancel);

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

          public static void main(String[] args) {
              Application.launch(LargeListViewIssue.class, args);
          }
      }
      -----------

            jgiles Jonathan Giles
            lianqli Lianqi Li (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported: