Reputation: 13
So basically I'm developing this Javafx app following the DAO pattern... and I want the floats to have that .00 end instead of .0 (to represents the balance.. money ) balance records in tableview with .0
here's how I initialized the tableview components
idC.setCellValueFactory(cellData -> cellData.getValue().idProperty().asObject());
balanceC.setCellValueFactory(cellData -> cellData.getValue().balanceProperty().asObject());
dateC.setCellValueFactory(cellData -> cellData.getValue().dateProperty());
timeC.setCellValueFactory(cellData -> cellData.getValue().timeProperty());
Upvotes: 1
Views: 1496
Reputation: 85
CellFactory isn't needed if you don't want to add more unnecessary code.
First, change your TableColumn<XXX,Double> balanceC
to TableColumn<XXX,String> balanceC
Then change your cell value factory to:
balanceC.setCellValueFactory(cellData -> cellData.getValue().balanceProperty().asString("%.2f"));
Easy and cleaner way IMO.
Personally, I write custom CellFactory when I need more complex cells, for example: mixing images and other nodes inside the cell.
Upvotes: -1
Reputation: 209684
Use a cellFactory
in addition to your cellValueFactory
(I'm assuming balanceC
is a TableColumn<T, Double>
for some type T
:
balanceC.setCellFactory(c -> new TableCell<>() {
@Override
protected void updateItem(Double balance, boolean empty) {
super.updateItem(balance, empty);
if (balance == null || empty) {
setText(null);
} else {
setText(String.format("%.2f", balance.doubleValue());
}
}
});
You can add currency symbols and use more sophisticated formatting if needed.
Upvotes: 2