java - Swing start simulation from Swing interface and write from thread. How? -
the problem lost myself in code. first of all, project supposed simulate shop queue. now, made swing interface data. when simulate button pressed, want start simulation. trick i'm not sure how write in fields without creating new interface.
my interface code (i left thought important):
public class interfata extends jframe { public void updatetextfield(string name, int i) { switch(i){ case 0: this.txtfield8.settext(name); break; case 1: this.txtfield9.settext(name); break; case 2: this.txtfield10.settext(name); break; case 3: this.txtfield11.settext(name); break; case 4: this.txtfield12.settext(name); break; case 5: this.txtfield7.settext(name); break; default : this.txtfield7.settext(name); break; } } private interfata() { mybutton = new jbutton("simulate"); mybutton.addactionlistener(new actionlistener() { public void actionperformed(actionevent e) { magazin shop = new magazin(tonumber(txtfield6.gettext()), tonumber(txtfield1.gettext()), tonumber(txtfield2 .gettext()), tonumber(txtfield3.gettext()), tonumber(txtfield4.gettext()), tonumber(txtfield5 .gettext())); shop.simulation(); } }); } public static void main(string[] args) { interfata interfatamea = new interfata(); interfatamea.settitle("shop simulator"); interfatamea.setsize(1000, 700); interfatamea.setdefaultcloseoperation(jframe.exit_on_close); interfatamea.setvisible(true); } }
i create thread in class magazin
have simulation()
methode:
public void simulation() { (int = 0; < nrclienti; i++) { int aux = shortestqueue(cozi); client c = generateclient(); new threads(cozi[aux], c, aux, avgwaittime).start(); } }
threads extends thread , here code:
public class threads extends thread { private coada coada; private client c; private int i, avgwaittime; public threads(coada q, client c, int aux, int avgwaittime) { this.coada = q; this.c = c; this.i = aux; this.avgwaittime = avgwaittime; } @override public void run() { coada.enque(c); int s = 0; (int = 0; < coada.getsize(); i++){ s = s + coada.getclient().getservtime(); } avgwaittime=avgwaittime+s; try { sleep(s); } catch (interruptedexception e) { // waiting finish service time e.printstacktrace(); } swingutilities.invokelater(new runnable() { public void run() { intf.updatetextfield(coada.tostring(), i); } }); coada.deque(); swingutilities.invokelater(new runnable() { public void run() { intf.updatetextfield(coada.tostring(), i); } }); } }
it has insert object in queue, show queue in jtextfield, remove object , reshow. found invokelater example, require making new interface intf. or not, i'm lost. please help. thank time!
p.s. if made unclear or forgot mention something, please tell.
the trick i'm not sure how write in fields without creating new interface.
the problem you're passing contents of textfields magazin
when create start simulation. afterwards (i.e., runs) magazin
has no way update textfields. instead, try passing reference interfata
magazin
stores in final
field variable. possible use variable inside invokelater
update textfields. possible text in fields using invokeandwait
. in general, invokeandwait
can used in place of invokelater
when there's no downside blocking calling thread short while.
Comments
Post a Comment