I'm trying to use a SwingWorker to perform a lengthy task and update a JLabel with the result:
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
new SwingWorker<String, Void>() {
@Override
protected String doInBackground() throws Exception {
return doCalculation();
}
protected void done() {
try {
label.setText(get());
} catch (InterruptedException e) {
System.out.println("thread was interrupted");
} catch (ExecutionException e) {
System.out.println("there was an ExecutionException");
}
}
}.execute();
}
});
I can click the button many times as I like and the Gui will remain responsive until two threads are finished at which point the Gui freezes while threads are running. If I only run one thread at a time, this problem still occurs.
I'd be greatful if anyone could point out if I am using the SwingWorker incorrectly or if there is another problem I'm not aware of. Thanks for your time. Ian
Edit
The doCalculation() is just something time consuming:
private String doCalculation() {
for (int i = 0; i < 10000000; i++) {
Math.pow(3.14, i);
}
return threads++ + "";
}