JavaFX:setWrapText(true)(WordWrap)在ListView中不起作用

时间:2012-04-01 13:00:58

标签: java javafx javafx-2

在ListView-Cell中激活WordWrap时,文本不会换行。这是一个例子:

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();
}

}

通常,单元格中的文本应该换行,但整个文本在一行中,并显示一个水平滚动条。

如何在列表单元格中使用WordWrap或者这是一个JavaFX错误?

2 个答案:

答案 0 :(得分:9)

有完全相同的问题。这是适用于我的代码。

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();
    }
}

答案 1 :(得分:2)

看起来像ListCell中的错误,作为一种解决方法,您可以使用ListView<Text>代替:

    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();

如果您想要可调整大小的列表,可以使用绑定:

    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);
    }