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

[ListView] ListView not downsizing cells when its breadth shrinks

    XMLWordPrintable

    Details

      Description

      I have a cell that binds it's preferred width to the width of the ListView. When I widen the ListView, the cell expands accordingly. But when I shrink the ListView, the cell never shrinks.

      This may (or may not) have to do with this comment in VirtualFlow.java:

          * - If the width and/or height has been reduced (but neither of
          * them has been expanded), then we simply have to reposition and
          * resize the scroll bars

      So this might have been intended as a feature, but I see it more as a bug.

      My use case is a ListView of TextFlows and I want to have the TextFlows wrapped at the the ListView's width. (I am developing CodeAreaFX[1] and a user asked for line wrapping, which would get us quite close to a rich text area.)

      Here is the code:

      import javafx.application.Application;
      import javafx.scene.Scene;
      import javafx.scene.control.ListCell;
      import javafx.scene.control.ListView;
      import javafx.scene.layout.Region;
      import javafx.scene.layout.StackPane;
      import javafx.scene.text.Text;
      import javafx.scene.text.TextFlow;
      import javafx.stage.Stage;

      public class Test extends Application {

          private static class MyCell extends ListCell<String> {
              public MyCell(ListView<String> lv) {
                  setMaxWidth(Region.USE_PREF_SIZE);
                  prefWidthProperty().bind(lv.widthProperty());
              }

              @Override
              protected void updateItem(String item, boolean empty) {
                  super.updateItem(item, empty);
                  setGraphic(empty ? null : new TextFlow(new Text(item)));
              }
          }

          @Override
          public void start(Stage stage) {
              ListView<String> listView = new ListView<>();
              listView.setCellFactory(lv -> new MyCell(lv));
              listView.getItems().add("This is a very long line that needs to be wrapped");

              StackPane stack = new StackPane();
              stack.getChildren().add(listView);
              Scene scene = new Scene(stack, 200, 100);
              stage.setScene(scene);
              stage.show();
          }

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


      If I run it and then reduce the window width, the cell with the TextFlow never shrinks.


      [1] https://github.com/TomasMikula/CodeAreaFX

        Attachments

          Issue Links

            Activity

              People

              Assignee:
              msladecek Martin Sládeček
              Reporter:
              tmikula Tomas Mikula
              Votes:
              0 Vote for this issue
              Watchers:
              4 Start watching this issue

                Dates

                Created:
                Updated:
                Resolved:
                Imported: