1

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());
Miss Chanandler Bong
  • 4,081
  • 10
  • 26
  • 36
ilyesky
  • 13
  • 5
  • 2
    I believe your question has already been asked and answered. [TableView column data set to 2 decimal places](https://stackoverflow.com/questions/34917983/tableview-column-data-set-to-2-decimal-places) and also [JavaFX format double in TableColumn](https://stackoverflow.com/questions/48733121/javafx-format-double-in-tablecolumn). You are expected to search for answers before posting a question. I recommend you read [How much research effort is expected of Stack Overflow users?](https://meta.stackoverflow.com/questions/261592/how-much-research-effort-is-expected-of-stack-overflow-users) – Abra Mar 18 '20 at 20:23
  • Abra I read them but they didn't work – ilyesky Mar 18 '20 at 22:05

2 Answers2

2

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.

James_D
  • 201,275
  • 16
  • 291
  • 322
-1

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.

GaRzY
  • 65
  • 1
  • 4
  • definitely wrong - a column should represent a property of the item, a cell is responsible for the visual representation of that property – kleopatra Nov 25 '21 at 23:52
  • @kleopatra I understand you , but javadoc of TableColumn Says: ```Type parameters: – The type of the TableView generic type (i.e. S == TableView) – The type of the content in all cells in this TableColumn.``` So the type T does not have to be related to the item. The accepted answer really work with string also (setText(String.format("%.2f", balance.doubleValue());) – GaRzY Nov 26 '21 at 00:16
  • @kleopatra you said `a cell is responsible for the visual representation of that property.` But the visual representation of that property, in the accepted answer, is String. What's the problem then? (accepted answer is doing 'setText('string')). – GaRzY Nov 26 '21 at 09:50
  • the problem is that this answer does it in the wrong place – kleopatra Nov 26 '21 at 09:57
  • 1
    what is the right place? I am here to learn and improve my skills, I just want to help others... sorry if I'm breaking some rules... – GaRzY Nov 26 '21 at 14:52
  • you are not breaking rules, you are just doing it the wrong way :) – kleopatra Nov 26 '21 at 17:01