java - Trying to use a thread with JProgressBar -
so i'm trying learn how use threads decided make program adds 1 waits 1/2 sec. while thread (that think made correctly) refreshes value of progress bar. i'm not sure if i've made program wrong or if it's getting stuck somewhere. put println in thread , get:
thred 0 1 2 3 4 5 6 7 8 9 10 11 (ect...) here frame code:
import java.awt.eventqueue; import java.awt.event.actionevent; import java.awt.event.actionlistener; import javax.swing.jbutton; import javax.swing.jframe; import javax.swing.jlabel; import javax.swing.jpanel; import javax.swing.jprogressbar; import javax.swing.border.emptyborder; public class frame extends jframe implements actionlistener{ private static final long serialversionuid = 1l; private jpanel contentpane; public static void main(string[] args) { frame f = new frame(); f.setvisible(true); f.setsize(450,120); } /** * create frame. */ public jprogressbar bar; public frame() { setdefaultcloseoperation(jframe.exit_on_close); setbounds(100, 100, 449, 120); contentpane = new jpanel(); contentpane.setborder(new emptyborder(5, 5, 5, 5)); setcontentpane(contentpane); contentpane.setlayout(null); bar = new jprogressbar(); bar.setstringpainted(true); bar.setbounds(6, 50, 438, 32); contentpane.add(bar); jlabel lblnewlabel = new jlabel( "percent of loop completion"); lblnewlabel.setbounds(6, 6, 279, 16); contentpane.add(lblnewlabel); jbutton btnstart = new jbutton("start"); btnstart.setbounds(327, 1, 117, 29); btnstart.addactionlistener(this); contentpane.add(btnstart); } public int i, progress; public void actionperformed(actionevent e) { updater u = new updater(); u.start(); for( =0; < 100; i++){ progress = i; try { thread.sleep(500); } catch (interruptedexception e1) { e1.printstacktrace(); } system.out.println(i); } } } and think thread class:
public class updater extends thread { public void run() { system.out.println("thred"); frame f = new frame(); int p = f.progress; while (p != 100) { f.bar.setvalue(p); } } }
you blocking event dispatching thread. responsible for, amongst other things, processing paint updates. while block thread, there no way updates can occur, meaning looks program has come stand still...
public void actionperformed(actionevent e) { updater u = new updater(); u.start(); // blocking, no more repaints or event notifications until finish... for( =0; < 100; i++){ progress = i; try { thread.sleep(500); } catch (interruptedexception e1) { e1.printstacktrace(); } system.out.println(i); } } the other problem have fact swing (for part) not thread-safe. is, expected updates , interactions ui occur within context of edt.
while there number of ways around it, simplest use swingworker, designed allow execute code in background thread , re-sync updates ui safely.
take at...
- progress bar java
- how create progress bar while file transfering
- jprogressbar isn't progressing
- how use swing timer delay loading of progress bar
for examples.
you may want take @ concurrency in swing more details
Comments
Post a Comment