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

[VirtualFlow] Cells with a height depending on their width are not correctly resized and positioned

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: P3 P3
    • 8u40
    • 8u20, 8u40
    • javafx
    • Windows 7 SP 1 64bit
      Java 8u20 (32bit) released version
      Java 8u40 b15 (32bit)

      When using a FlowPane inside the cells of a ListView, the cells are not always of the correct size (e.g. not high enough for a vertical ListView) and at the correct position (e.g. cells overlap vertically for a vertical ListView). This seems to be only an issue when a scrollbar is displayed.

      Below is a piece of code which should exemplify the described behavior.

      Explanation for the example:
      When executed, the application should show a ListView whose cells overlap and whose height is too low (-> only the upper half of the last line of each cell is shown and the bottom red border is not displayed at all). As soon as the window is resized to be wider, the layout is corrected. However, when the width of the window is adjusted again such that a further line is necessary for the text (but would be not if the width was 1 or 2 pixels more), the wrong layout should show up again.

      Possible source of error:
      The method fitCells() of the class VirtualFlow adjusts the size of the cells when scrollbars are displayed. However, it adjusts only one of the dimensions without recalculating the other one. Shouldn't the method fitCells() recalculate the other dimension like the method resizeCellSize() does?
      Additionally, the size of the cells is adjusted after their position was determined. Shouldn't the method fitCells() be called before the positioning happens in the method updateScrollBarsAndCells()?

      It would be good to know whether these assumptions are correct and whether a workaround exists.

      import java.util.LinkedList;
      import java.util.List;

      import javafx.application.Application;
      import javafx.collections.FXCollections;
      import javafx.collections.ObservableList;
      import javafx.scene.Scene;
      import javafx.scene.control.Label;
      import javafx.scene.control.ListCell;
      import javafx.scene.control.ListView;
      import javafx.scene.layout.FlowPane;
      import javafx.stage.Stage;

      public class ListViewWithFlowPaneTester extends Application {

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

          @Override
          public void start(Stage primaryStage) {
              ListView<Integer> listView = new ListView<>();

              ObservableList<Integer> observableList = FXCollections.observableArrayList();
              listView.setItems(observableList);
              listView.setCellFactory(param -> new MyCell());

              for (int i = 0; i < 10; i++) {
                  observableList.add(20);
              }

              Scene scene = new Scene(listView, 300, 200);
              primaryStage.setScene(scene);

              primaryStage.show();
          }

          private static class MyCell extends ListCell<Integer> {

              private FlowPane flowPane;

              private MyCell() {
                  flowPane = new FlowPane();
                  flowPane.setPrefWrapLength(30);
                  flowPane.setStyle("-fx-border-width: 1; -fx-border-color: red");
                  setGraphic(flowPane);
              }

              @Override
              protected void updateItem(Integer item, boolean empty) {
                  super.updateItem(item, empty);
                  if (empty || item == null) {
                      flowPane.getChildren().clear();
                      return;
                  }

                  List<Label> labels = new LinkedList<>();
                  flowPane.getChildren().clear();
                  for (int i = 0; i < item; i++) {
                      labels.add(new Label("Hallo"));
                  }
                  flowPane.getChildren().addAll(labels);
              }
          }
      }

            jgiles Jonathan Giles
            duke J. Duke
            Votes:
            1 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported: