我想要更新列表单元格的样式类,具体取决于可以在另一个面板中编辑的字段。Javafx更新对象属性ListCell
我尝试了以下,但在编辑该字段后,它改变了样式,但它也改变了列表上其他ListCells的样式。
private ListView<ProviderProduct> importProductsListView = //Set items in list
importProductsListView.setCellFactory(
new Callback<ListView<ProviderProduct>, ListCell<ProviderProduct>>() {
public ListCell<ProviderProduct> call(ListView<ProviderProduct> param) {
final ListCell<ProviderProduct> cell = new ListCell<ProviderProduct>(){
public void updateItem(ProviderProduct providerProduct, boolean empty){
super.updateItem(providerProduct, empty);
if(!empty) {
setText(providerProduct.toString());
if(providerProduct.hasPriceWarning()){
getStyleClass().add(Consts.CSS_ALERT);
}else{
getStyleClass().remove(Consts.CSS_ALERT);
}
providerProduct.priceListinoProperty().addListener(
new ChangeListener<BigDecimal>() {
@Override
public void changed(ObservableValue<? extends BigDecimal> observable,BigDecimal oldValue, BigDecimal newValue) {
if(providerProduct.hasPriceWarning()){
if(!getStyleClass().contains(Consts.CSS_ALERT)){
getStyleClass().add(Consts.CSS_ALERT);
}
}else{
getStyleClass().removeAll(Collections.singleton(Consts.CSS_ALERT));
}
}
});
}else{
setText("");
getStyleClass().remove(Consts.CSS_ALERT);
}
}
};
return cell;
}
});
伟大的反应和更好的方法来监听单元格项目的变化,而不是直接向字段。我最后添加了'PseudoClass',我使用javafx 8来管理样式的更新,它简化并删除了所有的条件。 – alexll 2014-11-02 13:01:10