Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have 1 class file Nepokretnost.java in which constructor look like this:

public Nepokretnost(String tipNepokretnosti, String zona, String pravo, 
        double povrsina, int amortizacija, double osnovica, double kredit, double porez) {
    this.tipNepokretnosti = tipNepokretnosti;
    this.zona = zona;
    this.pravo = pravo;
    this.povrsina = povrsina;
    this.amortizacija = amortizacija;
    this.osnovica = osnovica;
    this.kredit = kredit;
    this.porez = porez; 
}

Further, I have TableView with column for each of class fields. My problem is double field "povrsina". I want to set it from TextField.

I sent contents of TextField named txtPovrsina to double variable:

double dPovrsina;
dPovrsina = Double.parseDouble(txtPovrsina.getText());

then put all fields in TableView:

ObservableList<Nepokretnost> data = tblTabela.getItems();
    data.add(new Nepokretnost(cboNepokretnost.getValue().toString(),cboZona.getValue().toString(),
            txtPravo,dPovrsina,40,450000.25,2500.00,2500.00));

All is working well, but I want some behavior of app I can't figure out how to set. Right now when I put int like 25 in TextField I get 25.0 in TableView column. I want all column cells to have exactly 2 decimal places.

I tried:

DecimalFormat df=new DecimalFormat("#.00");
ObservableList<Nepokretnost> data = tblTabela.getItems();
    data.add(new Nepokretnost(cboNepokretnost.getValue().toString(),cboZona.getValue().toString(),
            txtPravo,df.format(dPovrsina),40,450000.25,2500.00,2500.00));

but I get error "Incompatible types: String cannot be converted to double"

I am still noob in java but my guess is format is making String and I want to input stay double just to have 2 decimal places. Like this column I will have same issue with other double fields.

Can anyone give me some direction please?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
196 views
Welcome To Ask or Share your Answers For Others

1 Answer

You want to change the way the data is displayed in your table, not change the data itself. To do that, you need to set a cell factory on your table column. Something like

TableView<Nepokretnost> table = new TableView<>();
TableColumn<Nepokretnost, Number> povrsinaCol = new TableColumn<>("Povrsina");
povrsinaCol.setCellValueFactory(cellData -> 
    new ReadOnlyDoubleWrapper(cellData.getValue().getPovrsina()));

povrsinaCol.setCellFactory(tc -> new TableCell<Nepokretnost, Number>() {
    @Override
    protected void updateItem(Number value, boolean empty) {
        super.updateItem(value, empty) ;
        if (empty) {
            setText(null);
        } else {
            setText(String.format("%0.2f", value.doubleValue()));
        }
    }
});

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...