在TableView中使用自定义CellFactory的Javafx 2.0慢速列

时间:2011-12-11 18:02:58

标签: java javafx javafx-2

我的uitableView中有这个自定义Cell Factory。滚动时,该列异常缓慢。任何理由都是这样,我该如何改进呢。

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 个答案:

答案 0 :(得分:5)

你应该开始做的两件事是:

1)而不是调用setStyle(...),调用getStyleClass()。add(...),然后使用外部CSS文件来定义样式类。在运行时解析CSS很慢并且可以避免显示。

2)重用VBox和Label,而不是每次调用updateItem时重新创建它。通过在updateItem方法之外移动VBox和Label来执行此操作(但将其保留在新的TableCell&lt;&gt;()括号中。

然而......更进一步,我怀疑你需要一个VBox或一个标签。只需在单元格本身上设置样式表,然后使用setText(...)将单元格设置为单元格。

- 乔纳森

相关问题