更改行(javafx)的背景色(或仅更改颜色)

更改行(javafx)的背景色(或仅更改颜色),javafx,tableview,javafx-8,Javafx,Tableview,Javafx 8,我有一个表视图。我想根据某些条件更改行的背景色。例如,如果balance(getBalance())小于零,则将该行的背景色设置为红色。这是我的setCellValueFactory: tc_proj_number.setCellValueFactory(cellData -> new SimpleStringProperty(cellData.getValue().getId().toString())); tc_proj_date.setCellValueFactory(cellDat

我有一个
表视图
。我想根据某些条件更改行的背景色。例如,如果balance(
getBalance()
)小于零,则将该行的背景色设置为红色。这是我的
setCellValueFactory

tc_proj_number.setCellValueFactory(cellData -> new SimpleStringProperty(cellData.getValue().getId().toString()));
tc_proj_date.setCellValueFactory(cellData -> new SimpleStringProperty(cellData.getValue().getValueDate().toInstant().atZone(ZoneId.systemDefault()).toLocalDate().toString()));
tc_proj_amount.setCellValueFactory(cellData -> new SimpleStringProperty(cellData.getValue().getBalance().setScale(2).toPlainString()));
tc_proj_comment.setCellValueFactory(cellData -> new SimpleStringProperty(cellData.getValue().getComment()));
使用方法。
请尝试以下代码(未测试):

tc项目金额设置单元工厂(列->{
返回新的TableCell(){
@凌驾
受保护的void updateItem(字符串项,布尔值为空){
super.updateItem(项,空);
如果(项==null | |空){
setText(空);
}否则{
setText(项目);
//使用不同的颜色设置balance<0的行的样式。
BigDecimal余额=新的BigDecimal(项目);
TableRow currentRow=getTableRow();
如果(balance.compareTo(BigDecimal.valueOf(0))小于0{
currentRow.setStyle(“-fx背景色:红色;”);
}else currentRow.setStyle(“”);
}
}
};
});
使用方法。
请尝试以下代码(未测试):

tc项目金额设置单元工厂(列->{
返回新的TableCell(){
@凌驾
受保护的void updateItem(字符串项,布尔值为空){
super.updateItem(项,空);
如果(项==null | |空){
setText(空);
}否则{
setText(项目);
//使用不同的颜色设置balance<0的行的样式。
BigDecimal余额=新的BigDecimal(项目);
TableRow currentRow=getTableRow();
如果(balance.compareTo(BigDecimal.valueOf(0))小于0{
currentRow.setStyle(“-fx背景色:红色;”);
}else currentRow.setStyle(“”);
}
}
};
});
使用方法。
请尝试以下代码(未测试):

tc项目金额设置单元工厂(列->{
返回新的TableCell(){
@凌驾
受保护的void updateItem(字符串项,布尔值为空){
super.updateItem(项,空);
如果(项==null | |空){
setText(空);
}否则{
setText(项目);
//使用不同的颜色设置balance<0的行的样式。
BigDecimal余额=新的BigDecimal(项目);
TableRow currentRow=getTableRow();
如果(balance.compareTo(BigDecimal.valueOf(0))小于0{
currentRow.setStyle(“-fx背景色:红色;”);
}else currentRow.setStyle(“”);
}
}
};
});
使用方法。
请尝试以下代码(未测试):

tc项目金额设置单元工厂(列->{
返回新的TableCell(){
@凌驾
受保护的void updateItem(字符串项,布尔值为空){
super.updateItem(项,空);
如果(项==null | |空){
setText(空);
}否则{
setText(项目);
//使用不同的颜色设置balance<0的行的样式。
BigDecimal余额=新的BigDecimal(项目);
TableRow currentRow=getTableRow();
如果(balance.compareTo(BigDecimal.valueOf(0))小于0{
currentRow.setStyle(“-fx背景色:红色;”);
}else currentRow.setStyle(“”);
}
}
};
});

项和空代表什么?在我的例子中,它总是返回null和false。对不起,我完了。我已经删除了我的SetCellValueFactory并使用了你的,它们应该一起使用。非常感谢。物品和空物品代表什么?在我的例子中,它总是返回null和false。对不起,我完了。我已经删除了我的SetCellValueFactory并使用了你的,它们应该一起使用。非常感谢。物品和空物品代表什么?在我的例子中,它总是返回null和false。对不起,我完了。我已经删除了我的SetCellValueFactory并使用了你的,它们应该一起使用。非常感谢。物品和空物品代表什么?在我的例子中,它总是返回null和false。对不起,我完了。我已经删除了我的SetCellValueFactory并使用了你的,它们应该一起使用。谢谢
tc_proj_amount.setCellFactory(column -> {
    return new TableCell<Account, String>() {
        @Override
        protected void updateItem(String item, boolean empty) {
            super.updateItem(item, empty);

            if (item == null || empty) {
                setText(null);
            } else {
                setText(item);
                // Style row where balance < 0 with a different color.
                BigDecimal balance = new BigDecimal(item);
                TableRow currentRow = getTableRow();

                if (balance.compareTo(BigDecimal.valueOf(0)) < 0) {            
                    currentRow.setStyle("-fx-background-color: red;");
                } else currentRow.setStyle("");
            }
        }
    };
});