问题描述
我在调整包含围绕TableCell项目的文本项的TableView时遇到问题。调整大小后,隐藏的值会调整大小,但可见项目不会重新计算文本换行。
I am experiencing an issue when resizing a TableView which contains text items that wrap around TableCell items. Upon resizing, hidden values are resized but the visible items do not re-calculate the text wrapping.
红色框中的推文在调整大小期间被隐藏,文本包装按预期调整。在调整大小阶段可以看到方框上方的推文,但仍然有旧包装。
The tweets in the red box were hidden during the resize and had their text wrapping adjusted as expected. Tweets above the box were visible during the resize phase and still have the old wrapping.
以下是调整大小阶段的代码。
Below is my code for the resize phase.
fxSearchResultsTableTweet.setCellFactory(new Callback<TableColumn<Status, String>, TableCell<Status, String>>() {
@Override
public TableCell<Status, String> call(TableColumn<Status, String> arg0) {
return new TableCell<Status, String>() {
private Text text;
@Override
public void updateItem(String item, boolean empty) {
super.updateItem(item, empty);
if (!isEmpty()) {
text = new Text(item.toString());
text.setWrappingWidth(fxSearchResultsTableTweet.getWidth());
this.setWrapText(true);
setGraphic(text);
}
}
};
}
});
}
我们非常感谢任何帮助。
Any help would be greatly appreciated.
推荐答案
这更接近但不是很好:
textCol.setCellFactory(new Callback<TableColumn<Status, String>, TableCell<String, String>>() {
@Override
public TableCell<Status, String> call(
TableColumn<Status, String> param) {
TableCell<Status, String> cell = new TableCell<>();
Text text = new Text();
cell.setGraphic(text);
cell.setPrefHeight(Control.USE_COMPUTED_SIZE);
text.wrappingWidthProperty().bind(cell.widthProperty());
text.textProperty().bind(cell.itemProperty());
return cell ;
}
});
在2.2中,当您向表中添加新项目时,显示错误的高度,然后调整单元格的大小大小正确。在8中它几乎是完美的,在添加第一个项目之后似乎失败了(至少在我的模型中)。
In 2.2 this displays the wrong height when you add new items to the table, then on resize the cells are sized correctly. In 8 it's almost perfect, just seems to fail after the first item is added (at least in my mock-up).
如评论中所述,
textCol.setCellFactory(tc -> {
TableCell<Status, String> cell = new TableCell<>();
Text text = new Text();
cell.setGraphic(text);
cell.setPrefHeight(Control.USE_COMPUTED_SIZE);
text.wrappingWidthProperty().bind(textCol.widthProperty());
text.textProperty().bind(cell.itemProperty());
return cell ;
});
似乎效果更好。
这篇关于JavaFX TableColumn文本包装的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!