map - JavaFx: populate TableView with ObservableMap -
i coudn't find in internet right answer, how can populate tableviw observablemap mapproperty. show articles in tableview sorted value.
public class article {      private mapproperty<string, integer> article = new simplemapproperty<>();            public final observablemap<string, integer> gearticle() {             return article.get();         }          public final void setarticle(observablemap<string, integer> value) {             article.set(value);         }          public mapproperty<string, integer> articleproperty() {             return article;         }     }    public class tablecontroller extends vbox implements initializable{      @fxml private tableview<article> tableview;     @fxml private tablecolumn<article, string> article;     @fxml private tablecolumn<article, integer> count;      ......      @override         public void initialize(url location, resourcebundle resources) {             article.setcellvaluefactory(new propertyvaluefactory<article, string>("article"));             count.setcellvaluefactory(new propertyvaluefactory<article, integer>("count"));     }   }      
one simple way of doing loop on keyset , create list of articles:
observablelist<article> list =  fxcollections.observablearraylist();      for(string key : article.keyset()){         article art = new article(key, article.get(key));         list.add(art);     }   and set table items list.
and if want sort list, implement in article comparable, , use collections.sort(list);
public class article implements comparable<article> {        //...         public int compareto(article comparearticle ) {                //ascending order               return this.value - comparearticle.getvalue();         }      }      
Comments
Post a Comment