5 Replies Latest reply: Mar 5, 2013 7:45 AM by James_D RSS

    ListView not initially displaying items.

    Bluewizard
      Hello,

      Our main application creates a ListView but does not initially display it. Over the course of running the application, items are being placed into the observable list of the ListView. When an item is placed into the list we tell the ListView to scrollTo (itemIndex). This ensures that the last item inserted is in view. At this point the ListiView has not been displayed to the user. This list view is part of a pane that has been loaded from an FXML file.

      When the user selects to look at the ListView for the first time - the list is seemingly empty - but if you adjust the size of the window, the items magically appear. From that point forward, you can hide/show the ListView and everything works as expected.

      If I take out the scrollTo call - then all the items display correctly the first time and each of the other times.

      Is there something that I should be doing the ListView upon creation?

      Thanks.
        • 1. Re: ListView not initially displaying items.
          JonathanGiles
          I'm a bit foggy on the details, but I'm fairly sure this is something that I have fixed in JavaFX 8.0 already. If you were able to provide a small test application I could test.

          -- Jonathan
          • 2. Re: ListView not initially displaying items.
            jsmith
            Please include full source so that somebody could reproduce this easily.
            http://sscce.org/
            • 3. Re: ListView not initially displaying items.
              Bluewizard
              Hello,

              Below is the standalone code that demonstrates the problem. When the GUI comes up please do the following:

              1) press the button "1. - Add 100" - it will add 100 rows to the ListView which has been allocated but not displayed.
              2) press the button "2. - Show ListView" - it will place the ListView to the center of the border pane making it visible. You will see that no rows are being displayed but the scroll-bar is show.
              3) press the button "3. - Add 1" - it will add a single row to the end of the ListView. You will now see all the rows displayed and the scroll-bar correctly calculated.

              If you comment out the myListView.scrollTo(myListView.getItems().size() - 1); in the Add 100 button EventHandler - the list will display immediately, as expected. It would see that the scrollTo method is getting in the way.

              Thanks.

              import javafx.application.Application;
              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.ToolBar;
              import javafx.scene.layout.BorderPane;
              import javafx.stage.Stage;
              
              /**
               * @author omey
               */
              public class ListViewUpdateIssue extends Application
              {
                   /**
                    * @param args
                    */
                   public static void main(String[] args)
                   {
                        // Launch the main application
                        Application.launch(args);
                   }
              
                   @Override
                   public void start(Stage primaryStage) throws Exception
                   {
                        final BorderPane mainPane = new BorderPane();
              
                        final ListView<String> myListView = new ListView<>();
              
                        Button add100 = new Button("1. - Add 100");
                        add100.setOnAction(new EventHandler<ActionEvent>()
                        {
              
                             @Override
                             public void handle(ActionEvent event)
                             {
                                  for (int ii = 0; (ii < 100); ii++)
                                  {
                                       // Add the item
                                       myListView.getItems().add("Item : " + ii);
                                       
                                       // Select the last line - if you comment this line out everything 
                                       // will work as expected
                                       myListView.scrollTo(myListView.getItems().size() - 1);
                                  }
              
                             }
                        });
              
                        Button showListView = new Button("2. - Show ListView");
                        showListView.setOnAction(new EventHandler<ActionEvent>()
                        {
              
                             @Override
                             public void handle(ActionEvent event)
                             {
                                  mainPane.setCenter(myListView);
              
                             }
                        });
              
                        Button add1 = new Button("3. - Add 1");
                        add1.setOnAction(new EventHandler<ActionEvent>()
                        {
              
                             @Override
                             public void handle(ActionEvent event)
                             {
                                  // Add the row that will make things show up
                                  myListView.getItems().add("The line that will make everything appear");
                                  
                                  // Select the last line
                                  myListView.scrollTo(myListView.getItems().size() - 1);
                             }
                        });
              
                        // Add the tool bar of buttons
                        mainPane.setTop(new ToolBar(add100, showListView, add1));
              
                        // Create the main scene
                        Scene mainScene = new Scene(mainPane, 600, 400);
              
                        // put it together and show the window
                        primaryStage.setScene(mainScene);
                        primaryStage.centerOnScreen();
                        primaryStage.show();
              
                   }
              
              }
              • 4. Re: ListView not initially displaying items.
                csh
                I am having exactly the same issue which is related to the scrollTo method, but in TableView. It isn't always reproducable but often.
                I assume it has the same root cause.

                If there's any workaround let me know, please.

                It would also be helpful to link the Jira issue.
                • 5. Re: ListView not initially displaying items.
                  James_D
                  I tried this with Java 1.7.0_13 and the bundled JavaFX (2.2.4?) and it reproduced the error you describe.

                  I then tried it with JDK 1.8.0-ea-b77 and the list displayed immediately after step 2. The layout was a little off, however, so it didn't quite scroll all the way to the bottom of the list. I think the original bug is fixed but there may be a second (layout-associated) bug in there.