java - Having trouble with JTable and JModel -
this clanmodel class. table based on tree map.
public class clanmodel extends abstracttablemodel{ private map<integer, clanmember> clanmembers; private final string[] columnnames = {"name", "rank"}; clanmodel(map<integer, clanmember> clanmembers){ this.clanmembers = clanmembers; } @override public int getrowcount() { return clanmembers.size(); } @override public int getcolumncount() { return 2; } @override public string getcolumnname(int column){ return columnnames[column]; } @override public object getvalueat(int row, int column) { if(column == 0) return clanmembers.get(row).getname(); if(column == 1) return clanmembers.get(row).getrank(); return 1; } @override public void setvalueat(object value, int row, int column) { if(column == 0) clanmembers.get(row).setname((string) value); if(column == 1) clanmembers.get(row).setrank((rank) value); firetablecellupdated(row, column); } }
this in gui constructor
clantable = new jtable(new clanmodel(clanmembers)); panel.add(clantable);
the map filled on button press working. however, table doesn't appear before clicking button or after. error every time resize gui window.
i have rough idea of need fix hasn't been working out.
try moving setvisible()
it's after add()
, pack()
. see initial threads.
public static void main(string[] args) { eventqueue.invokelater(new runnable() { @override public void run() { jframe f = new jframe(); f.setdefaultcloseoperation(jframe.exit_on_close); jtable table = new jtable(new clanmodel(…)); f.add(new jscrollpane(table)); f.pack(); f.setlocationrelativeto(null); f.setvisible(true); } }); }
Comments
Post a Comment