3 Replies Latest reply: Apr 26, 2013 5:09 AM by csh RSS

    How to wrap text in a ListView?

    csh
      How can I wrap text in a ListView?

      The following code does not work. I want the text to wrap and prevent horizontal scrollbars.
      public class TestApp3 extends Application {
      
          public static void main(String[] args) {
              try {
                  launch(args);
              } catch (Exception e) {
                  e.printStackTrace();
              }
          }
      
          public void start(final Stage stage) throws Exception {
      
      
              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) {
                      ListCell<String> cell = new ListCell<String>() {
      
                          protected void updateItem(String item, boolean empty) {
                              super.updateItem(item, empty);
      
                              if (!empty) {
      
      // Both of these methods don't work.
                                  setText(item);
                                  setWrapText(true);
      
                                  Label label = new Label(item);
                                  label.setWrapText(true);
                                  setGraphic(label);
      
                              }
                          }
                      };
                      return cell;
                  }
              });
      
      
              Scene scene = new Scene(listView);
              stage.setScene(scene);
              stage.show();
          }
      }
        • 1. Re: How to wrap text in a ListView?
          shakir.gusaroff
          It works with a text:
            Text label = new Text(item);
             label.setWrappingWidth(listView.getWidth() - 10);
             setGraphic(label);
          • 2. Re: How to wrap text in a ListView?
            csh
            Hm, it works a little, but it is not satisfying:

            a) The Text node looks very blurry, worse readable than the text in Label.

            b) When I resize the ListView (Stage in my example), eventually horizontal scrollbars will appear, despite of binding the wrappingLengthProperty. It seems the ListView's width can only increase, but not decrease from there.
            import javafx.application.Application;
            import javafx.collections.FXCollections;
            import javafx.scene.Scene;
            import javafx.scene.control.Control;
            import javafx.scene.control.Label;
            import javafx.scene.control.ListCell;
            import javafx.scene.control.ListView;
            import javafx.scene.layout.FlowPane;
            import javafx.scene.layout.HBox;
            import javafx.scene.layout.Priority;
            import javafx.scene.text.Text;
            import javafx.stage.Stage;
            import javafx.util.Callback;
            
            public class TestApp 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) {
                            ListCell<String> cell = new ListCell<String>() {
            
                                protected void updateItem(String item, boolean empty) {
                                    super.updateItem(item, empty);
                                    if (!empty) {
                                        Text text = new Text(item);
                                        text.wrappingWidthProperty().bind(listView.widthProperty().subtract(10));
                                        setGraphic(text);
                                    }
                                }
                            };
                            return cell;
                        }
                    });
            
                    Scene scene = new Scene(listView);
                    stage.setScene(scene);
                    stage.show();
                }
            }
            • 3. Re: How to wrap text in a ListView?
              csh
              i reported a bug:
              https://javafx-jira.kenai.com/browse/RT-29997

              Here's another workaound, but still horizontal scrollbars:
              Label label = new Label(item);
              label.setWrapText(true);
              label.maxWidthProperty().bind(listView.widthProperty());
              Group group = GroupBuilder.create().children(label).build();
              setGraphic(group);