java - Refreshing/updating JTable with setValueAt doesn't work correctly -
i'm working on project college , need 2 of jframe. first 1 main menu , second 1 visible when jbutton(run) pressed. need paint memory in both of jframe, used jtable show memory.
memory class is:
public class memory extends jpanel{ private jpanel panel; private jtable table; private string[] array; private jscrollpane scrollpane; public memory() { array = new string[256] this.addmemorygui(); } public final jpanel addmemorygui() { this.panel = new jpanel(); this.panel.setpreferredsize(new dimension(315,490)); this.panel.setlayout(null); this.add(panel); string info[][] = new string[256][2]; for(int j=0;j<256;j++) { info[j][0] = j; info[j][1] = null; } string[] title = new string[]{"address","value"}; defaulttablemodel model = new defaulttablemodel(info,title); table = new jtable(model){ @override public boolean iscelleditable(int rowindex, int colindex) { return false; } }; defaulttablecellrenderer centerrenderer = new defaulttablecellrenderer(); centerrenderer.sethorizontalalignment( jlabel.center ); table.getcolumnmodel().getcolumn(0).setcellrenderer( centerrenderer ); table.getcolumnmodel().getcolumn(1).setcellrenderer( centerrenderer ); jtableheader header = table.gettableheader(); header.setbackground(color.gray); this.scrollpane = new jscrollpane(this.table); this.scrollpane.setbounds(0, 0,315, 490); this.panel.add(this.scrollpane); return panel; } public void setaddrvalue(int addr,string value) { memory.this.array[addr] = value; this.table.setvalueat(value,addr , 1); } public string getaddrvalue(int addr) { return memory.this.array[addr]; } public string[] getmemory() { return memory.this.array; } public void deletevalue(int i) { array[i]=null; this.table.setvalueat(null, i, 1); } }
i add jtable main jframe , jcomponents:
public class maingui extends jframe{ private jtextfield field1; private jtextfield field2; private jbutton button1; private jbutton button2; private memory mem; public maingui() { this.graphiccomponents(); } public final void graphiccomponents() { this.settitle("machine"); this.setsize(800, 620); this.setlayout(null); this.setdefaultcloseoperation(jframe.exit_on_close); this.setresizable(false); this.field1=new jtextfield(); this.field1.setbounds(10,50,70,20); this.add(field1); this.field2=new jtextfield(); this.field2.setbounds(90,50,70,20); this.add(field2); this.button1=new jbutton("write value"); this.button1.setbounds(170,50,130,20); this.button1.addactionlistener(new actionlistener() { @override public void actionperformed(actionevent e) { maingui.this.mem.setaddrvalue(maingui.this.field1.gettext().tostring(),maingui.this.field2.gettext().tostring() ); } }); this.add(button1); this.button2 = new jbutton("run"); this.button2.setbounds(500,550,100,30); this.button2.addactionlistener(new actionlistener() { @override public void actionperformed(actionevent e) { executiongui exe = new executiongui(mem); exe.setvisible(true); } }); this.add(button2); this.mem = new memory(); this.mem.setbounds(450, 30, 315, 490); this.add(mem);
} }
here in code, setvalueat() method work perfect , there no problem. when use same code of jtabel in second jframe when press run jbutton create new jtable not in memory class , insert value jtable using setvalueat(), jtabel doesn't update.
code of second jframe:
public class executiongui extends jframe { private jtextfield field1; private jtextfield field2; private jbutton button1; private jscrollpane scrollpane; private jbutton button2; private memory mem; private jtable table; private static defaulttablemodel model; private string info[][]; private string[] title; private memory mem; public executiongui(memory mem) { this.mem = mem; this.graphiccomponents(); } public final void graphiccomponents() { this.settitle("run"); this.setbounds(200, 100, 800, 600); this.setlayout(null); this.setdefaultcloseoperation(jframe.exit_on_close); this.setresizable(false); this.info = new string[256][2]; for(int j=0;j<256;j++) { info[j][0] = j; if(mem.getaddrvalue(j)==null) { } else { info[j][1] = mem.getaddrvalue(j); } } title = new string[]{"address","value"}; model = new defaulttablemodel(info,title); table = new jtable(model){ @override public boolean iscelleditable(int rowindex, int colindex) { return false; } }; defaulttablecellrenderer centerrenderer = new defaulttablecellrenderer(); centerrenderer.sethorizontalalignment( jlabel.center ); table1.getcolumnmodel().getcolumn(0).setcellrenderer( centerrenderer ); table1.getcolumnmodel().getcolumn(1).setcellrenderer( centerrenderer ); jtableheader header = table.gettableheader(); header.setbackground(color.gray); this.scrollpane = new jscrollpane(table); this.scrollpane.setbounds(610, 30,170, 470); this.add(this.scrollpane); this.field1=new jtextfield(); this.field1.setbounds(10,50,70,20); this.add(field1); this.field2=new jtextfield(); this.field2.setbounds(90,50,70,20); this.add(field2); this.button1=new jbutton("write value"); this.button1.setbounds(170,50,130,20); this.button1.addactionlistener(new actionlistener() { @override public void actionperformed(actionevent e) { table.setvalueat(maingui.this.field2.gettext().tostring(),maingui.this.field1.gettext().tostring(),1 ); } }); this.add(button1); }
so have problem second jframe , can't refresh jtable new data. have tried more 1 way take right result there nothing.
thanks help.
Comments
Post a Comment