I've a TableView
(located inside a ScrollPane
), one of the rows is as follows:
tc_proj_amount.setCellValueFactory(cellData ->
new SimpleStringProperty(cellData.getValue().getBalance().toPlainString()));
I want to color the whole row where tc_proj_amount
is negative. For this purpose I use this:
tc_proj_amount.setCellFactory(column -> new TableCell<Transaction, 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);
if (balance.compareTo(BigDecimal.valueOf(0)) < 0) {
TableRow currentRow = getTableRow();
currentRow.setStyle("-fx-background-color: tomato;");
}
}
}
});
When I run my app, rows are colured as needed, but when I scroll up-down a list more and more rows become coloured, even when tc_proj_amount
is not negative. When I try to sort columns, all rows become coloured. How can I fix this problem?
2nd question. I want to somehow format the value of tc_proj_amount
. For this purpose I did this:
DecimalFormat df = new DecimalFormat("#,###.00");
tc_proj_amount.setCellValueFactory(cellData ->
new SimpleStringProperty(df.format(cellData.getValue().getBalance()).toString()));
Part that changes row colour is the same as above. In this case I receive an exception. How can I fix it?
Exception in thread "JavaFX Application Thread" java.lang.NumberFormatException
Exception is pointing into part where rows are colored.