gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:42:14 25 4
gpt4 key购买 nike

在 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 错误??

最佳答案

遇到了完全相同的问题。这是对我有用的代码。

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

关于JavaFX:setWrapText(true) (WordWrap) 在 ListView 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9964579/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com