java - TableView column data set to 2 decimal places -


i have 1 class file nepokretnost.java in constructor 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, have tableview column each of class fields. problem double field "povrsina". want set textfield.

i sent contents of textfield named txtpovrsina double variable:

double dpovrsina; dpovrsina = double.parsedouble(txtpovrsina.gettext()); 

then put 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 working well, want behavior of app can't figure out how set. right when put int 25 in textfield 25.0 in tableview column. want column cells have 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 error "incompatible types: string cannot converted double"

i still noob in java guess format making string , want input stay double have 2 decimal places. column have same issue other double fields.

can give me direction please?

you want change way data displayed in table, not change data itself. that, need set cell factory on table column. 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()));         }     } }); 

Comments

Popular posts from this blog

SVG stroke-linecap doesn't work for circles in Firefox? -

routes - Laravel 4 Wildcard Routing to Different Controllers -

cross browser - XSLT namespace-alias Not Working in Firefox or Chrome -