我想测试每次呼叫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;
}
}