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

ListView/ListCell: setWrapText/setMaxWidth has no effect

XMLWordPrintable

      setWrapText has no effect on the cell, nor has setMaxWidth

      I've come up with a bad workaround. Please have a look.

      My expectation would have been, that the text wraps and there are no horizontal scrollbars unless really necessary (e.g. a word can't be wrapped further).

      It also doesn't work, if you set a Label as graphic.

      Tested with 2.2 and 8.0 b86 (same behavior)

      See the code and my comment for workaround.



      import javafx.application.Application;
      import javafx.collections.FXCollections;
      import javafx.geometry.Insets;
      import javafx.geometry.Pos;
      import javafx.scene.Group;
      import javafx.scene.GroupBuilder;
      import javafx.scene.Scene;
      import javafx.scene.control.Label;
      import javafx.scene.control.ListCell;
      import javafx.scene.control.ListView;
      import javafx.scene.control.OverrunStyle;
      import javafx.scene.layout.HBox;
      import javafx.scene.layout.Priority;
      import javafx.scene.layout.VBox;
      import javafx.stage.Stage;
      import javafx.util.Callback;

      public class TestApp3 extends Application {

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

          public void start(final Stage stage) throws Exception {

              final ListView<String> listView = new ListView<String>();

              listView.setItems(FXCollections.observableArrayList("This is very very very very very very very very very very very very very very very long text."));

              listView.setCellFactory(new Callback<ListView<String>, ListCell<String>>() {
                  @Override
                  public ListCell<String> call(ListView<String> stringListView) {
                      final ListCell<String> cell = new ListCell<String>() {

                          protected void updateItem(String item, boolean empty) {
                              super.updateItem(item, empty);
                              if (!empty) {

                                  setText(item);
                                  setWrapText(true);

                                  // Setting maxWidth has no effect either.
                                  setMaxWidth(100);

                                  // This is my workaround, which works better, but is not satisfying because there is still unnecessary space in the cell and horizontal scroll bars.
      // Label label = new Label(item);
      // label.setWrapText(true);
      // label.maxWidthProperty().bind(listView.widthProperty());
      // Group group = GroupBuilder.create().children(label).build();
      // setGraphic(group);
                              }
                          }
                      };
                      return cell;
                  }
              });

              Scene scene = new Scene(listView);
              stage.setScene(scene);
              stage.show();
          }
      }

            Unassigned Unassigned
            cschudtjfx Christian Schudt (Inactive)
            Votes:
            2 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated:
              Imported: