Reputation: 3808
I have this custom CellFactory in my tableView. When Scrolling, that column is exceptionally slow. Any reason why this is so and how can I improve it.
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;
}
});
Upvotes: 1
Views: 2063
Reputation: 666
Two things you should start by doing are:
1) Rather than call setStyle(...), call getStyleClass().add(...), and then use an external CSS file to define style classes. Parsing CSS at runtime is slow and show be avoided.
2) Reuse VBox and Label, rather than recreating it each time updateItem is called. Do this by moving VBox and Label outside of the updateItem method (but keep it within the new TableCell<>() brace.
However....taking 2) a bit further, I doubt you need either a VBox or a Label. Just set the styleclass on the cell itself, and set the text against the cell using setText(...).
-- Jonathan
Upvotes: 5