我的 tableView 中有这个自定义 CellFactory。滚动时,该列特别慢。为什么会这样,我该如何改进。
lastTradeColumn.setCellFactory(
new Callback<TableColumn<Stock, Price>,TableCell<Stock, Price>>(){
@Override public TableCell<Stock, Price> call( TableColumn<Stock, Price> p ) {
TableCell<Stock, Price> cell = new TableCell<Stock, Price>() {
@Override public void updateItem(Price price, boolean empty) {
super.updateItem(price, empty);
if (price != null) {
VBox vbox = new VBox(5);
vbox.getChildren().add(new Label("£"+price.toString()));
if( price.getOldPrice() > price.getNewPrice()) {
vbox.setStyle("-fx-background-color:#EA2A15;");
}
else if( price.getOldPrice() < price.getNewPrice()) {
vbox.setStyle("-fx-background-color:#9CF311;");
}
setGraphic( vbox );
}
}
};
return cell;
}
});
最佳答案
您应该首先做的两件事是:
1) 不要调用 setStyle(...),而是调用 getStyleClass().add(...),然后使用外部 CSS 文件来定义样式类。在运行时解析 CSS 很慢,应避免显示。
2) 重复使用 VBox 和 Label,而不是每次调用 updateItem 时都重新创建它们。通过将 VBox 和 Label 移到 updateItem 方法之外(但将其保留在新的 TableCell<>() 大括号内)来执行此操作。
但是....进一步考虑 2),我怀疑您是否需要 VBox 或 Label。只需在单元格本身上设置样式类,然后使用 setText(...) 针对单元格设置文本。
-- 乔纳森
关于TableView 中带有自定义 CellFactory 的 Javafx 2.0 慢列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8466215/