java swing addTaskListener
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 comp开发者_如何学Ciler 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
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
.
精彩评论