Binding hashmap with tableview (JavaFX) -
i want display hashmap contents in javafx tableview. please find below code used set hashmap contents table columns. problem i'm having it's displaying 1 row. for loop iterating 5 times: each time picking first value of hashmap.
if ignore return simpleobjectproperty line, for loop iterating on content in hashmap.
final observablelist<map> data = fxcollections.observablearraylist(); data.addall(hashmap); tablecolumn<map.entry, string> ncol = new tablecolumn<map.entry, string>("name"); ncol.seteditable(true); ncol.setcellvaluefactory(new callback<tablecolumn.celldatafeatures<entry, string>, observablevalue<string>>() { @override public observablevalue<string> call(tablecolumn.celldatafeatures<entry, string> p) { set <string> set=hashmap.keyset(); (string key:hashmap.keyset()) { string key1= key.tostring(); return new simpleobjectproperty<>(key.tostring()); } return null; } }); table.setitems(data); table.getcolumns().setall(ncol,.........);
cellfactory.callback.call()creates 1 cell, not cells in loop- using
returnloop breaks loop execution.
take @ next example, comments:
public class maptableview extends application { @override public void start(stage stage) { // sample data map<string, string> map = new hashmap<>(); map.put("one", "one"); map.put("two", "two"); map.put("three", "three"); // use detailed type map.entry<string, string> tablecolumn<map.entry<string, string>, string> column1 = new tablecolumn<>("key"); column1.setcellvaluefactory(new callback<tablecolumn.celldatafeatures<map.entry<string, string>, string>, observablevalue<string>>() { @override public observablevalue<string> call(tablecolumn.celldatafeatures<map.entry<string, string>, string> p) { // callback returns property 1 cell, can't use loop here // first column use key return new simplestringproperty(p.getvalue().getkey()); } }); tablecolumn<map.entry<string, string>, string> column2 = new tablecolumn<>("value"); column2.setcellvaluefactory(new callback<tablecolumn.celldatafeatures<map.entry<string, string>, string>, observablevalue<string>>() { @override public observablevalue<string> call(tablecolumn.celldatafeatures<map.entry<string, string>, string> p) { // second column use value return new simplestringproperty(p.getvalue().getvalue()); } }); observablelist<map.entry<string, string>> items = fxcollections.observablearraylist(map.entryset()); final tableview<map.entry<string,string>> table = new tableview<>(items); table.getcolumns().setall(column1, column2); scene scene = new scene(table, 400, 400); stage.setscene(scene); stage.show(); } public static void main(string[] args) { launch(); } }
Comments
Post a Comment