views:

34

answers:

2

I am new to java and have a swing task question.

Can I add a listener to a currently running task? For instance if something happens in doInBackGround I want to add a listener for finished and display a dialog. I have tried but the compiler doesnt like me. :)

Something like.

private class MyTask extends Task<Void, Void>{
  @Override
  public void doInBackground(){
     if(foo == foo){
        this.addTaskListener(new TaskListener() {
          public void taskFinsished(){}...
         });
     }
  }
}

Thanks

A: 

What is a Task class? What is a TaskListener class?

Kirill
This would make a decent comment, but is not an answer.
Chadwick
-1 JSR-296 Look it up here: http://java.sun.com/developer/technicalArticles/javase/swingappfr/
Romain Hippeau
A: 

Task is not a listener-oriented component. You need to override one or more of it's methods to get the results. All of these methods will execute on the EDT.

cancelled() - The cancel() method was called to terminate the task.

succeeded(T result) - The Task completed, and result holds the return value from doInBackground().

interrupted(InterruptedException e) - interrupt was called on the Thread executing the Task.

failed(Throwable cause) - The doInBackground threw an exception.

finished() - The Task has finished (in some fashion). Think of this as the functional equivalent of finally for Tasks.

Devon_C_Miller