multithreading - Using Threads with Java Swing -
i'm building user-interface java's swing library, , i've run problem.
when updating text-area in long method, there pause while method runs, , text area updated @ once, instead of little bit @ time.
i managed fix problem using new thread in following manner:
private void jbuttonactionperformed(java.awt.event.actionevent evt) { thread x = new thread() {public void run() { // things update text area }}; x.start(); }
this works having text area update in small pieces, opposed @ once.
however, problem method can called once or java crashes. read has being able create thread 1 time. how can modify code keep desired functionality?
any appreciated.
well, doubt codes crashing because of thread
creation, because each time method called, you're creating new thread
(you can't restart existing instance of thread
has been started (even if it's completed or not)), cause you're getting concurrent modification exception because swing not thread safe...
probably easiest solution use swingworker
allow execute long running task in background, provide easy use functionality update ui safely, through publish
, process
, done
methods.
do prevent possible issues, might consider disabling button until load action completed, prevent people mashing button spawning multiple background processes...
Comments
Post a Comment