JavaFX Tableview not binding/reverting -


looking in diagnosing behavior, i'm @ loss may occurring.

on table edit, labels update, values don't ever seem to, whenever table updated values revert. happens sorting, , can shown true via:

for(row r : elementstable.getitems().get(0))    system.out.println(r.getsymbol() + r.getweight() + r.getatom()); 

gui:

@fxml private textfield row1; @fxml private textfield row2; @fxml private textfield row3; @fxml private tableview<row> elementstable; private list<row> row; private observablelist<row> orow;  @override public void initialize(url url, resourcebundle rb) {     // todo     row = new arraylist(); }    private void addrow(actionevent event) {      elementstable.getitems().add(new row(          row1.gettext(),          row2.gettext(),          row3.gettext())); }  public void construct(list<string> z, list<string> w, list<string> z) throws exception{     //populate table rows     for(int = 0; < z.size(); i++){         row.add(new row(z.get(i), w.get(i), a.get(i)));     }     orow = fxcollections.observablearraylist(row);     elementstable.setitems(orow);  } 

fxml

  <tableview fx:id="elementstable" editable="true" layoutx="67.0" layouty="124.0" prefheight="237.0" prefwidth="523.0">      <columns>         <tablecolumn fx:id="elementcol" editable="true" prefwidth="138.0" text="element">            <cellfactory>               <textfieldtablecell fx:factory="fortablecolumn" />            </cellfactory>            <cellvaluefactory>               <propertyvaluefactory property="symbol" />            </cellvaluefactory>         </tablecolumn>         <tablecolumn fx:id="weightcol" editable="true" prefwidth="191.0" text="weight fraction">            <cellfactory>               <textfieldtablecell fx:factory="fortablecolumn" />            </cellfactory>            <cellvaluefactory>               <propertyvaluefactory property="weight" />            </cellvaluefactory>         </tablecolumn>         <tablecolumn fx:id="atomcol" editable="true" prefwidth="189.0" text="atom fraction">            <cellfactory>               <textfieldtablecell fx:factory="fortablecolumn" />            </cellfactory>            <cellvaluefactory>               <propertyvaluefactory property="atom" />            </cellvaluefactory>         </tablecolumn>      </columns>      <items>         <fxcollections fx:factory="observablearraylist">            <row atom="" symbol="" weight="" />         </fxcollections>      </items>   </tableview> 

table model

public class row{     private final simplestringproperty symbol = new simplestringproperty("");     private final simplestringproperty weight = new simplestringproperty("");     private final simplestringproperty atom = new simplestringproperty("");      /**      * default constructor, defaults empty strings      */     public row(){         this("","","");     }     /**      *       * @param s symbol(or za)      * @param w weight fraction      * @param atom fraction (or atom count ending "#")      */     public row(string s, string w, string a){         setsymbol(s);         setweight(w);         setatom(a);     }      public string getsymbol(){return symbol.get();}     public string getweight(){return weight.get();}     public string getatom(){return atom.get();}      public void setsymbol(string s){symbol.set(s);}     public void setweight(string w){weight.set(w);}     public void setatom(string a){atom.set(a);} } 

state 1: before doing enter image description here

state 2: edited cells enter image description here

state 3: adding row table, data reverts edits enter image description here

the issue don't provide "property accessor" methods in row class. without these, propertyvaluefactory , textfieldtablecell cannot access actual properties , bind/listen them changes.so row class should like

public class row{     private final simplestringproperty symbol = new simplestringproperty("");     private final simplestringproperty weight = new simplestringproperty("");     private final simplestringproperty atom = new simplestringproperty("");      /**      * default constructor, defaults empty strings      */     public row(){         this("","","");     }     /**      *       * @param s symbol(or za)      * @param w weight fraction      * @param atom fraction (or atom count ending "#")      */     public row(string s, string w, string a){         setsymbol(s);         setweight(w);         setatom(a);     }      public string getsymbol(){return symbol.get();}     public string getweight(){return weight.get();}     public string getatom(){return atom.get();}      public void setsymbol(string s){symbol.set(s);}     public void setweight(string w){weight.set(w);}     public void setatom(string a){atom.set(a);}      public stringproperty symbolproperty() {         return symbol ;     }      public stringproperty weightproperty() {         return weight ;     }      public stringproperty atomproperty() {         return atom ;     }  } 

for more details, check api documentation propertyvaluefactory , tutorial section on properties (which general background on javafx property pattern).

if, reason, can't provide these methods (e.g. using existing classes cannot change model), can provide "wiring" between table column editing , model hand, using tablecolumn.setoneditcommit(...) in controller class. following should make things work existing row class, though recommend implementing model "property accessors" whenever possible.

public void initialize(url url, resourcebundle rb) {     // ...     elementcol.setoneditcommit(e -> {         int rowindex = e.gettableposition().getrow();         row row = elementstable.getitems().get(rowindex);         row.setelement(e.getnewvalue());     });      // other columns... } 

Comments

Popular posts from this blog

How to provide Authorization & Authentication using Asp.net, C#? -

toolbar - How to add link to user registration inside toobar in admin joomla 3 custom component -

How to use Authorization & Authentication in Asp.net, C#? -