swing - java.awt.EventQueue.invokeLater explained -
i curious why have use java.awt.eventqueue.invokelater
control swing components.
why can't in normal thread? going on behind scenes? have noticed if have jframe
can set visibility true or false main thread without getting errors, , seem work. achieve using java.awt.eventqueue.invokelater
? aware can use swingutilities.invokelater
explained here, seem 1 , same thing.
thanks explanation. valid question.
edit: answer wumpz question can create jframe
jframe frame = new jframe("hello world"); frame.setsize(new dimension(300, 300)); frame.setpreferredsize(new dimension(300, 300)); frame.setmaximumsize(new dimension(300, 300)); frame.setminimumsize(new dimension(300, 300)); frame.setvisible(true); frame.pack(); frame.setdefaultcloseoperation(windowconstants.exit_on_close);
and on same thread created following.
for (int = 0; < 34; i++) { system.out.println("main thread setting "+(!frame.isvisible())); frame.setvisible(!frame.isvisible()); }
and no complaints.
the complete swing processing done in thread called edt (event dispatching thread). therefore block gui if compute long lasting calculations within thread.
the way go here process calculation within different thread, gui stays responsive. @ end want update gui, have to done within edt. eventqueue.invokelater
comes play. posts event (your runnable
) @ end of swings event list , processed after previous gui events processed.
also usage of eventqueue.invokeandwait
possible here. difference is, calculation thread blocks until gui updated. obvious must not used edt.
be careful not update swing gui different thread. in cases produces strange updating/refreshing issues.
still there java code out there starts jframe simple main thread. cause issues, not prevented swing. modern ides create start gui:
public static void main(string args[]) { java.awt.eventqueue.invokelater(new runnable() { public void run() { new newjframe().setvisible(true); } }); }
Comments
Post a Comment