Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaFX: setWrapText(true) (WordWrap) doesn't work in ListView

When activating WordWrap in in ListView-Cell the text is not wrapping. Here is a example:

public class ListBug extends Application {

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

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

    myList.setCellFactory(new Callback<ListView<String>, ListCell<String>>() {

        @Override
        public ListCell<String> call(ListView<String> param) {
            return new ListCell<String>() {
                @Override
                public void updateItem(String item, boolean empty) {
                    setWrapText(true);
                    setText(item);
                    super.updateItem(item, empty);
                }
            };
        }
    });

    for (int i = 0; i < 100; i++) {
        myList.getItems()
                .add("Lorem ipsum dolor sit amet, consectetur adipisicing elit, "
                        + "sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. "
                        + "Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris "
                        + "nisi ut aliquip ex ea commodo consequat.");
    }
    primaryStage.setScene(new Scene(myList));
    primaryStage.setTitle("ListWrapBug");
    primaryStage.show();
}

}

Normally the text in the cells should wrap but the whole text is in one line an a horizontal scrollbar is shown.

How can I use WordWrap in a list cell or is this a JavaFX bug??

like image 847
Hendrik Ebbers Avatar asked Apr 01 '12 13:04

Hendrik Ebbers


2 Answers

Had exactly the same problem. Here is the code that worked for me.

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

    @Override
    public void start(Stage primaryStage) {
        ListView<String> myList = new ListView<String>();
        myList.setCellFactory(new Callback<ListView<String>, ListCell<String>>() {
            @Override
            public ListCell<String> call(final ListView<String> list) {
                return new ListCell<String>() {
                    {
                        Text text = new Text();
                        text.wrappingWidthProperty().bind(list.widthProperty().subtract(15));
                        text.textProperty().bind(itemProperty());

                        setPrefWidth(0);
                        setGraphic(text);
                    }
                };
            }
        });

        for (int i = 0; i < 100; i++) {
            myList.getItems()
                    .add("Lorem ipsum dolor sit amet, consectetur adipisicing elit, "
                            + "sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. "
                            + "Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris "
                            + "nisi ut aliquip ex ea commodo consequat.");
        }
        primaryStage.setScene(new Scene(myList));
        primaryStage.setTitle("ListWrapBug");
        primaryStage.show();
    }
}
like image 55
gleb Avatar answered Nov 03 '22 09:11

gleb


Look like a bug in ListCell, as a workaround you can use ListView<Text> instead:

    ListView<Text> myList = new ListView<Text>();

    for (int i = 0; i < 100; i++) {
        myList.getItems().add(TextBuilder.create().text("Lorem ipsum dolor sit amet, consectetur adipisicing elit, "
                + "sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. "
                + "Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris "
                + "nisi ut aliquip ex ea commodo consequat.").wrappingWidth(200).build());
    }

    myList.setPrefWidth(200);
    primaryStage.setScene(new Scene(myList));
    primaryStage.setTitle("ListWrapBug");
    primaryStage.show();

If you want resizable list you can use binding:

    ListView<Text> myList = new ListView<Text>();

    for (int i = 0; i < 100; i++) {
        Text text = new Text("Lorem ipsum dolor sit amet, consectetur adipisicing elit, "
                + "sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. "
                + "Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris "
                + "nisi ut aliquip ex ea commodo consequat.");
        text.wrappingWidthProperty().bind(myList.widthProperty());
        myList.getItems().add(text);
    }
like image 25
Sergey Grinev Avatar answered Nov 03 '22 07:11

Sergey Grinev