java - JavaFX format SimpleLongProperty in TableView -


i'm stuck trying format long values in tableview javafx.

i have following class store rows want display on table:

    import java.text.decimalformat;      import javafx.beans.property.simpledoubleproperty;     import javafx.beans.property.simplelongproperty;     import javafx.beans.property.simplestringproperty;      public class databycurrencypairrow {          private decimalformat integerformat = new decimalformat("#,###");          private simplestringproperty currencypair = new simplestringproperty("");         private simpledoubleproperty shareoftotalvolume = new simpledoubleproperty(0);         private simplelongproperty totalvolume = new simplelongproperty(0);         private simplelongproperty currencybought = new simplelongproperty(0);         private simplelongproperty currencysold = new simplelongproperty(0);         private simplelongproperty monthlyaverage = new simplelongproperty(0);          public databycurrencypairrow() {             currencypair.set("");             shareoftotalvolume.set(0);             totalvolume.set(0);             currencybought.set(0);             currencysold.set(0);             monthlyaverage.set(0);         }          public string getcurrencypair() {             return currencypair.getvalue();         }          public void setcurrencypair(string currencypair) {             this.currencypair.setvalue(currencypair);         }          public long getmonthlyaverage() {             return monthlyaverage.getvalue();         }          public void setmonthlyaverage(long monthlyaverage) {             this.monthlyaverage.setvalue(monthlyaverage);         }          public long getcurrencysold() {             return currencysold.getvalue();         }          public void setcurrencysold(long currencysold) {             this.currencysold.setvalue(currencysold);         }          public long getcurrencybought() {             return currencybought.getvalue();         }          public void setcurrencybought(long currencybought) {             this.currencybought.setvalue(currencybought);         }          public long gettotalvolume() {                   return totalvolume.getvalue();         }          public void settotalvolume(long totalvolume) {             this.totalvolume.setvalue(totalvolume);         }          public double getshareoftotalvolume() {             return shareoftotalvolume.getvalue();         }          public void setshareoftotalvolume(double shareoftotalvolume) {             this.shareoftotalvolume.setvalue(shareoftotalvolume);         }      } 

then have controller initialize method have been trying override updateitem method table show comma thousand separator:

public class maincontroller {      private static final string default_time_horizon = new string("0");     private final numberformat integerformat = new decimalformat("#,###");       @fxml     tableview<databycurrencypairrow> tabletransactionsbycurrencypair;      @fxml     tablecolumn<databycurrencypairrow, long> columntotal;       @fxml     void initialize() {          columntotal.setcellfactory(                 new callback<tablecolumn<databycurrencypairrow, simplelongproperty>, tablecell<databycurrencypairrow, simplelongproperty>>() {                      @override                     public tablecell<databycurrencypairrow, simplelongproperty> call(tablecolumn<databycurrencypairrow, simplelongproperty> param                     ) {                         return new tablecell<databycurrencypairrow, simplelongproperty>() {                              @override                             protected void updateitem(simplelongproperty item, boolean empty) {                                 super.updateitem(item, empty);                                  if (item == null || empty) {                                     settext("0");                                     setstyle("");                                 } else {                                     settext(integerformat.format(item.longvalue()));                                 }                             }                          };                     }                 }         ); 

and method populates tableview:

    public void updatebycurrencypairtable() {         system.out.println("#maincontroller: updating data in table view markets volumes currency pair");          observablelist<databycurrencypairrow> data = tabletransactionsbycurrencypair.getitems();         data.clear();          // add row items table view markets volume currency         (databycurrencypairrow row : customer.getdatabycurrencypairr12m().getdatabycurrencypair()) {             data.add(row);         }     } 

please me showing how this!! tried override updateitem method long instead of simplelongproperty , ide accepted code still number not formatted in table.

thank guys in advance!!!

longproperty implements observablevalue<number>, not observablevalue<long> (or observablevalue<simplelongproperty>). table columns need of type tablecolumn<databycurrencypair, number> , cell factory needs match types accordingly.

here's simple example of formatted column longs:

import java.text.decimalformat; import java.text.numberformat; import java.util.random;  import javafx.application.application; import javafx.beans.property.longproperty; import javafx.beans.property.simplelongproperty; import javafx.beans.property.simplestringproperty; import javafx.beans.property.stringproperty; import javafx.scene.scene; import javafx.scene.control.tablecell; import javafx.scene.control.tablecolumn; import javafx.scene.control.tableview; import javafx.stage.stage;  public class tablewithformattedlong extends application {      private final numberformat integerformat = new decimalformat("#,###");      @override     public void start(stage primarystage) {         tableview<item> table = new tableview<>();         tablecolumn<item, string> itemcolumn = new tablecolumn<>("item");         itemcolumn.setcellvaluefactory(celldata -> celldata.getvalue().nameproperty());          tablecolumn<item, number> valuecolumn = new tablecolumn<>("value");         valuecolumn.setcellvaluefactory(celldata -> celldata.getvalue().valueproperty());          valuecolumn.setcellfactory(tc -> new tablecell<item, number>() {             @override             protected void updateitem(number value, boolean empty) {                 super.updateitem(value, empty);                 if (value == null || empty) {                     settext("");                 } else {                     settext(integerformat.format(value));                 }             }         });          table.getcolumns().add(itemcolumn);         table.getcolumns().add(valuecolumn);          random rng = new random();          (int = 1 ; <= 20 ; i++) {             table.getitems().add(new item("item "+i, rng.nextlong()));         }          primarystage.setscene(new scene(table, 600, 600));         primarystage.show();     }      public static class item {         private final stringproperty name = new simplestringproperty();         private final longproperty value = new simplelongproperty();          public item(string name, long value) {             setname(name);             setvalue(value);         }          public final stringproperty nameproperty() {             return this.name;         }           public final string getname() {             return this.nameproperty().get();         }           public final void setname(final string name) {             this.nameproperty().set(name);         }           public final longproperty valueproperty() {             return this.value;         }           public final long getvalue() {             return this.valueproperty().get();         }           public final void setvalue(final long value) {             this.valueproperty().set(value);         }        }      public static void main(string[] args) {         launch(args);     } } 

Comments

Popular posts from this blog

jOOQ update returning clause with Oracle -

java - Warning equals/hashCode on @Data annotation lombok with inheritance -

java - BasicPathUsageException: Cannot join to attribute of basic type -