java - GUI not updating after calling GUI method in another thread -


edit: simplified code.

this main class initialises gui , server threads.

public class main {     public static void main(string args[]) throws classnotfoundexception, instantiationexception, illegalaccessexception, unsupportedlookandfeelexception, unknownhostexception {     runnable tgui = new transceivergui();     runnable server = new server(tgui);     new thread(tgui).start();     new thread(server).start();     } } 

now, in public class transceivergui extends javax.swing.jframe implements runnable class, have method following:

protected boolean incomingfilerequest(string filename, long filesize, string user) throws interruptedexception, invocationtargetexception {         /* .... code logic executes .... */         /* update gui here not work */         labelprogress.setforeground(color.red);         labelprogress.settext("receive progress");     } 

whenever incomingfilerequest called within gui class (from eventlistener) works , gui updated.

however, when call incomingfilerequest server class/thread, code runs , returns correct value, gui not update.

the server thread calls this: // popup request boolean answer = gui.incomingfilerequest(message.getmessage(), message.getfilesize(), message.senderipaddress);

i have placed code updates gui in block creates new thread this:

thread t = new thread() { public void run() {    labelprogress.setforeground(color.red);    labelprogress.settext("receive progress");    repaint(); } }; t.start(); 

i tried javax.swing.swingutilities.invokelater(new runnable() { public void run() { /* update gui code */ } });.

my question is, how call method (that updates gui) within gui class thread? keeping in mind code executed perfectly, gui elements not updated.

any appreciated.

from post

whenever incomingfilerequest called within gui class (from eventlistener) works , gui updated.

however, when call incomingfilerequest server class/thread, code runs , returns correct value, gui not update.

that ppl tried explain in comments. gui updates must performed in gui thread - edt.

the event dispatch thread 1 handling "listener" why gui gets updated. update gui different thread have "schedule" changes performed in edt. that, wrap gui modification code runnable class , pass edt via swingutilities.invokelalater(runnable) method. way gui updated asap

you have mentioned that

i tried javax.swing.swingutilities.invokelater(new runnable() { public void run() { /* update gui code */ } });.

and result? because how should done. if not working you, there other porblem code either method body waits external updates, or scheduled in different moment think is.


Comments

Popular posts from this blog

dns - How To Use Custom Nameserver On Free Cloudflare? -

python - Pygame screen.blit not working -

c# - Web API response xml language -