我想要更新列表單元格的樣式類,具體取決於可以在另一個面板中編輯的字段。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