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

Unpredictable behaviour with scrollTo and the view classes (TableView as example)

    XMLWordPrintable

Details

    • Bug
    • Resolution: Unresolved
    • P4
    • tbd
    • 7u6
    • javafx
    • JavaFX 2.2b11 32-bit, Windows 7 64-bit

    Description

      It seems that scrollTo, when called at bad times causes all kinds of glitches; the code below reproduces one of them (no items being shown, despite there being 1000 times in the View -- doing a cursor down/up cures it). Other behaviour I've seen is the entire StackPane in which the View control resides not being drawn (including several other Panes, Labels, ImageViews...). Any kind of Animation, scrolling action, focusing action, etc, will cause a redraw and it will be as if nothing was wrong.

      Basically, what is happening is this:

      1) An Event occurs (in the example 'simulated' with a Platform.runLater() call that repeats every second, but it can also be a KeyEvent)
      2) In the Event Handler, a new View instance is created and populated.
      3) Still in the same Event Handler, the new View is added to the Scene.
      4) In a Platform.runLater() call, scrollTo is called to set the View in the correct position (calling it immediately after adding it to the Scene in the same Event as step 3 yields even worse results also worthy of investigation).

      My suspicion is that the View classes, when never rendered yet, are not ready to deal with a scrollTo event (they also seem to have problems with select and focus events). The wrapping in a Platform.runLater() call mitigates this a bit, but only if in between that call and the original Event there was sufficient time to render the View atleast once.

      It seems to have gotten worse in 2.2 for some reason, in 2.1 this problem never seems to occur when the scrollTo is wrapped in a Platform.runLater() call.

      Please also see the thread on the mailinglist titled "Debugging a UI refresh problem (not dirtyopts related it seems)", which is what triggered this investigation. Note that this example code does not exactly reproduce the problem (the entire StackPane not being drawn), but it comes close...


      package hs.disappearbug;

      import javafx.application.Application;
      import javafx.application.Platform;
      import javafx.beans.property.SimpleStringProperty;
      import javafx.beans.property.StringProperty;
      import javafx.scene.Scene;
      import javafx.scene.control.TableColumn;
      import javafx.scene.control.TableView;
      import javafx.scene.control.cell.PropertyValueFactory;
      import javafx.scene.layout.StackPane;
      import javafx.stage.Stage;

      public class CopyOfMain extends Application {
        public static void main(String[] args) {
          launch(args);
        }

        @Override
        public void start(Stage stage) throws Exception {
          final StackPane pane = new StackPane();

          Scene scene = new Scene(pane);

          stage.setScene(scene);
          stage.show();
          stage.setWidth(1000);
          stage.setHeight(700);

          new Thread() {
            @Override
            public void run() {
              for(;;) {
                Platform.runLater(new Runnable() {
                  @Override
                  public void run() {
                    pane.getChildren().clear();

                    final TableView<Person> tableView = new TableView<>();

                    TableColumn<Person, String> firstColumn = new TableColumn<>("First name");
                    TableColumn<Person, String> secondColumn = new TableColumn<>("Last name");
                    firstColumn.setCellValueFactory(new PropertyValueFactory<Person, String>("firstName"));
                    secondColumn.setCellValueFactory(new PropertyValueFactory<Person, String>("lastName"));
                    tableView.getColumns().add(firstColumn);
                    tableView.getColumns().add(secondColumn);

                    for(int i = 0; i < 1000; i++) {
                      tableView.getItems().add(new Person("John", "Hendrikx"));
                    }

                    pane.getChildren().add(tableView);

                    Platform.runLater(new Runnable() {
                      @Override
                      public void run() {
                        tableView.scrollTo(200); // Also try without wrapping this in Platform.runLater()
                      }
                    });
                  }
                });

                try {
                  Thread.sleep(1000);
                }
                catch(InterruptedException e) {
                  e.printStackTrace();
                }
              }
            }
          }.start();
        }

        public class Person {
          private StringProperty firstName;

          public Person(String firstName, String lastName) {
            setFirstName(firstName);
            setLastName(lastName);
          }

          public void setFirstName(String value) {
            firstNameProperty().set(value);
          }

          public String getFirstName() {
            return firstNameProperty().get();
          }

          public StringProperty firstNameProperty() {
            if(firstName == null) {
              firstName = new SimpleStringProperty(this, "firstName");
            }
            return firstName;
          }

          private StringProperty lastName;

          public void setLastName(String value) {
            lastNameProperty().set(value);
          }

          public String getLastName() {
            return lastNameProperty().get();
          }

          public StringProperty lastNameProperty() {
            if(lastName == null) {
              lastName = new SimpleStringProperty(this, "lastName");
            }
            return lastName;
          }
        }
      }

      Attachments

        Activity

          People

            Unassigned Unassigned
            jhendrikx John Hendrikx
            Votes:
            1 Vote for this issue
            Watchers:
            3 Start watching this issue

            Dates

              Created:
              Updated:
              Imported: