0
votes

I coudn't find in internet the right answer, how can I populate tableviw with ObservableMap as MapProperty. I would like to show the articles in a tableview sorted by 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"));
    }
  }
1
Have you gone through this example ? - ItachiUchiha
Yes but my problem is, how can I bind TableView with articleProperty() - Sam Joos

1 Answers

0
votes

One simple way of doing this is to loop over the keySet and create a 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 then you set the table Items to list.

And if you want to sort the list, implement in Article comparable, and use collections.sort(list);

public class Article implements Comparable<Article> {
       //...
        public int compareTo(Article compareArticle ) {

              //ascending order
              return this.value - compareArticle.getValue();
        }

    }