I am creating the UI for my application which shares a core module with versions for other platforms. In JavaFX, I'm trying to use Task
s to do things in the background, but I can't figure out how to update the Task state.
This is what I'm trying to do. The user
variable holds an instance of a class which performs xmlrpc requests:
public Task<Integer> doLogin()
{
return new Task<Integer>() {
@Override
protected Integer call()
{
user.login();
if (!user.getIsAuthorized())
{
// set the state to FAILED
}
else
{
// set the state to SUCCEDED
}
user.remember();
}
};
}
In my UI Thread I want to be able to do something like this to update my graph UI:
loginTask.setOnSucceeded(new EventHandler<WorkerStateEvent>() {
@Override
public void handle(WorkerStateEvent t) {
// perform an UI update here depending on the state t
}
});
How am I supposed to set the state? There is nothing that does that in the Task API.
Task
states are not intended to be used for user logic. They are introduced to controlTask
flow. To add user logic intoTask
you need to useresult
conception. In your case you may want to useTask<Boolean>
and result of your task will beTRUE
for correct credentials andFALSE
for incorrect:Task creation:
Starting that task: