Reputation: 39
I'm trying to let a JTable load from data stored in a ArrayList with HashMaps. The code looks something like
private ArrayList<HashMap> menuLijst;
private JPanel tablePanel;
//some init
tablePanel = new JPanel();
then, this is some random data, just to make sure it works.
this.menuList = new ArrayList();
// hashmap
HashMap hm1 = new HashMap();
HashMap hm2 = new HashMap();
hm1.put("question", "Contact");
hm1.put("query", "select contact from tim");
hm1.put("message", "contact");
hm2.put("question", "Sales");
hm2.put("query", "select sales from tim");
hm2.put("message", "sales");
menuList.add(hm1);
menuList.add(hm2);
so that's how my HashMap looks like, which is kind of superfluous to know, since the JTabel does not depend on pre-defined size-data.
All together, my question is, how to put data from the ArrayLists with HashMaps in the JTable.
I have tried some options but it didn't work out as it should be.
Thanks in advance.
Upvotes: 0
Views: 1415
Reputation: 47608
Simply extend AbstractTableModel and implement the following methods:
Choose the order of appearance of the columns (for example query then message), and use
HashMap map = menuLijst.get(row);
if (col==0)
return map.get("query");
else if (col==1)
return map.get("message");
to implement getValueAt(int row, int col)
Upvotes: 1
Reputation: 36611
Either you just create your own TableModel
implementation (or extend from one of the available classes), or you must use another data structure (see the available constructors on DefaultTableModel
).
But creating your TableModel
would be rather straight-forward with this data structure as soon as you find a mechanism to determine the column order as a HashMap
as no ordering.
The Swing table tutorial contains a section on creating TableModel
instances
Upvotes: 1
Reputation: 3533
jtable uses the javabeans concept, and hashmap key/value pair are not implemented as javabeans property. I would use a pair class or define your own pair class with key/value property
Upvotes: 0