events - Swing invokeandwait -
i learning swing , created sample gui. trying achieve following in exact order...
- the user enters text text fields.
- the user clicks "launch" button.
- the "launch" button becomes disabled.
- a background thread spawns , processes text text fields.
- the background thread finishes.
- the "launch" button becomes enabled again.
i trying use invokeandwait can seen below "cannot call invokeandwait event dispatcher thread". main method in same .class file , i'm not sure "event dispatcher thread" is. whats best approach this, need setup kind of alert in worker thread route "event dispatcher thread"?
launchbutton code
private void launchbuttonactionperformed(java.awt.event.actionevent evt) { launchbutton.setenabled(false); try { java.awt.eventqueue.invokeandwait(new mytestthread()); } catch (exception e) { } launchbutton.setenabled(true); }
worker thread
public class mytestthread extends thread { private int = 0; public void run() { while (i < 5) { try { system.out.println(i++); sleep(10); } catch (interruptedexception e) { e.printstacktrace(); } } } }
solution
worker thread
public class workerthread extends swingworker<integer[], void> { @override public integer[] doinbackground() { system.out.println("doing in background"); (int = 0; < 5; i++) { try { thread.sleep(1000); system.out.println("doing in background" + i); } catch (interruptedexception e) { e.printstacktrace(); } } return null; } @override public void done() { system.out.println("swingworker done"); } }
starting worker thread event dispatch thread (edt)
new workerthread().execute();
the event dispatch thread (edt) thread allowed access swing classes , (almost all) methods. initizalization , events of gui executed edt. execute in edt have use swingutilities.invokelater
or invokeandwait
(this equivalent eventqueue.invokexxx
). why swing programs start swingutilities.invokelater()
in main: execute gui initialization in edt.
while edt busy ui freezes that's why background threads useful. when need big amount of work independent ui (calculations, i/o, transmission, ...) have use "worker threads".
for more threading in swing see tutorial: http://docs.oracle.com/javase/tutorial/uiswing/concurrency/
now, trying accomplish right tools using aren't.
you need know 2 things: how handle events (like button presses) , how create background threads.
for first 1 see tutorial: http://docs.oracle.com/javase/tutorial/uiswing/components/button.html
you need add anactionlistener
button , whenever button throws event listener's actionperformed
method executed, in edt.
minimal example:
jbutton button = new jbutton("test button"); button.addactionlistener(new actionlistener() { public void actionperformed(actionevent event) { system.out.println("the button pressed."); } }
the event
variable contains useful information, example event.getsource() return object threw event, in case button.
now want when button pressed create worker thread. worker threads created using swingworker class, you've seen in concurrency tutorial. there can define piece of code executed in background thread (in doinbackground()
method) , piece of code executed in edt after work in background done (in done()
method).
so you'd want this:
private static jbutton _button; //... _button = new jbutton("test button"); _button.addactionlistener(new actionlistener() { public void actionperformed(actionevent event) { system.out.println("the button pressed."); _button.setenabled(false); swingworker worker = new swingworker() { @override protected object doinbackground() throws exception { //do useful in background thread return null; } @override protected void done() { _button.setenabled(true); } } worker.execute(); } }
there's lot of information out there this. read java reference classes, read official tutorials , search in so. tutorial swingworkers is: http://www.javacreed.com/swing-worker-example/
Comments
Post a Comment