java - Updating JTable on button click -


i working jtable custom table model found here. have updated code suggestions provided in post , have run new problem. change have made code inject arraylist<completedplayer> jtable avoid issues threads. after doing that, code update table pressing button has stopped working.

the code used initialize jtable following:

tablemodel model = new playertablemodel(filehandler.getcompletedplayers()); jtable table = new jtable(model); 

the code used update jtable following:

jbutton btnrefreshallplayers = new jbutton("refresh");  btnrefreshallplayers.addactionlistener(new actionlistener() {      public void actionperformed(actionevent arg0) {          playertablemodel model = (playertablemodel) table.getmodel();         model.firetabledatachanged();      }  }); 

i have tried using repaint() not work either. of right now, way jtable update close , reopen program. filehandler has arraylist using jtable increases in size user adds more players.

why doesn't firetabledatachanged() detect changes?

i have searched on stackoverflow , couple of people have said use method.

no, should not call firetablexxx methods outside of context of tablemodel itself, people suggesting otherwise wrong , cause issues in future. looks of code, nothing has changed. if you've updated tablemodel according answer provided in previous question, there no relationship data in model external source. need manually reload data external source, create new tablemodel , apply table

for example...

jbutton btnrefreshallplayers = new jbutton("refresh");  btnrefreshallplayers.addactionlistener(new actionlistener() {      public void actionperformed(actionevent arg0) {          tablemodel model = new playertablemodel(filehandler.getcompletedplayers());         table.setmodel(model);      }  }); 

i have tried setting new model updated arraylist , worked did not keep table row widths set.

this reasonable thing table do, because has no idea if new model has same properties/columns old, resets them.

you walk columnmodel, storing column widths in list or map before apply model , reapply widths

is there proper way update jtable?

you provide tablemodel refresh method, load data , trigger tabledatachanged event

public class playertablemodel extends abstracttablemodel {     private final list<playersummary.player> summaries;      public playertablemodel(list<playersummary.player> summaries) {         this.summaries = new arraylist<playersummary.player>(summaries);     }     // other tabelmodel methods...      public void refresh() {         summaries = new arraylist<>(filehandler.getcompletedplayers());         firetabledatachanged();     } } 

then need call method in actionlistener...

playertablemodel model = (playertablemodel)table.getmode(); model.refresh(); 

Comments