滚动时不调用 updateItem

updateItem is not called when scrolling

我正在将 table 从数据库加载到 table 视图。 我在基于 type/value 的特定列中使用自定义 CellFactory。基本上,我需要在高于阈值的类型为 Float 的列中突出显示单元格。 我看到这在第一次填充 table 视图时有效,但是当我滚动 table 或对列进行排序时,呈现混乱,即无法正常工作。 我注意到在滚动或排序时不会调用 updateItem。 如何使 TableView 渲染 "correct" 而不管滚动和排序?

这是我使用的代码部分:

                else
            {
                TableColumn col=new TableColumn(rs.getMetaData().getColumnName(i+1));   
                col.setCellValueFactory(new Callback<CellDataFeatures<ObservableList,Float>,ObservableValue<Float>>()
                    {   
                        @Override
                        public ObservableValue<Float> call(CellDataFeatures<ObservableList, Float> param) 
                        {                                                       
                            return new SimpleObjectProperty(param.getValue().get(j));                
                        } 
                    });                     
                col.setCellFactory(new Callback<TableColumn<ObservableList, ObservableValue<Float>>, TableCell<ObservableList, Float>>()
                {
                    @Override
                    public TableCell<ObservableList, Float> call(TableColumn<ObservableList,ObservableValue<Float>> param)
                    {
                        TableCell<ObservableList,Float> cell=new TableCell<ObservableList,Float>()
                        {                                
                            @Override                                
                            public void updateItem(Float item, boolean empty)
                            {                                                                        
                                super.updateItem(item, empty);
                                if(item!=null)
                                {                                                                                
                                    if(item>0.5)
                                    {
                                        setText(item.toString());
                                        setTextFill(Color.BLACK);  
                                        setFont(Font.font(null, FontWeight.BOLD, 12));
                                        setStyle("-fx-background-color:RED");
                                    }
                                    else
                                    {
                                        setText(item.toString());
                                        setTextFill(Color.BLACK); 
                                    }
                                }
                            }                                
                        };
                        return cell;
                    }                        
            });                       
                table.getColumns().addAll(col);
                col.prefWidthProperty().bind(table.prefWidthProperty().divide(10));
            }

您只需要实现您的逻辑即可正确处理所有情况。例如。如果单元格从大于 0.5 的值重复使用到小于 0.5 的值,则永远不会重置样式。

你需要

@Override                                
public void updateItem(Float item, boolean empty) {                                                                        
    super.updateItem(item, empty);
    if(item==null) {
        setText(null);
    } else {                                                                                
        setText(item.toString());
        setTextFill(Color.BLACK);  
        if(item>0.5) {
            setFont(Font.font(null, FontWeight.BOLD, 12));
            setStyle("-fx-background-color:RED");
        } else {
            setFont(Font.font(null, FontWeight.NORMAL, 12));
            setStyle("");
        }
    }
}