[問題]SwingWorker.setProgress()與PropertyChangeEvent

作者: kafy   2016-04-15 03:24:32
我想測試每次呼叫setProgress()是否都會產生一個progress的PropertyChangeEvent,
所以我在doInBackground()呼叫setProgress()100次,
希望可以在ProperyChangeListener catch到100次event,
可是下面的code跑出來的結果連一次都沒有執行到,
可以請好心人幫忙看一下是拿裡出了問題嗎?謝謝!
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import javax.swing.*;
public class ThreadTest {
public static void main(String[] args) {
new ProgBar().execute();
}
}
class ProgBar extends SwingWorker<Void, Void> {
public ProgBar() {
addPropertyChangeListener(new PropertyChangeListener() {
public void propertyChange(PropertyChangeEvent evt) {
if ("progress".equals(evt.getPropertyName())) {
int value = (Integer) evt.getNewValue();
System.out.println("propertyChange called with: " + value);
}
}
});
setProgress(0);
}
public Void doInBackground() throws InterruptedException {
int count = 0;
// simulate uploading files
while (count < 100){
count += 1;
setProgress(count);
Thread.sleep(4000);
}
return null;
}
}

Links booklink

Contact Us: admin [ a t ] ucptt.com