Search code examples
javajavafxjavafx-8bigdecimal

Formatting Bigdecimal. JavaFX


I want to print BigDecimal in TableColumn in my JavaFx application. But I can't properly format it.

I've tried this:

DecimalFormat df = new DecimalFormat("#,###.00");
tc_ma_sell_amount.setCellValueFactory(cellData -> new SimpleStringProperty(df.format(cellData.getValue().getSellAmount()).toString()));

Format is ok, it prints 1 005 689.56. But problem here is when I sort table according to this column in my application, it refers to this values as if they are Strings and sorting is not correct.

I made following changes:

tc_ma_sell_amount.setCellValueFactory(cellData -> new SimpleObjectProperty<BigDecimal>(cellData.getValue().getSellAmount()));

Here, format is not ok. (1005689.5600), but sorting works correctly. What do I have to change in order to have right format("#,###.00") and sorting?


Solution

  • I did it myself.

    tc_ma_sell_amount.setCellFactory(param -> {
                return new TableCell<ConversionDeals, BigDecimal>(){
                    @Override
                    protected void updateItem(BigDecimal item, boolean empty) {
                        super.updateItem(item, empty);
                        if(empty || item == null){
                            setText("");
                        } else {
                            setText(df.format(item));
                        }
                    }
                };
            });