スイングバックグラウンドタスクの実行について学習したばかりで、実験を始めていますが、実装に少し問題があります。私のコードは画像を取得し、画像の取得が成功した結果=0か失敗した結果=-1かを示す結果(整数)を返します。これが私の問題です。結果を取得するのが早すぎます。結果がdone()メソッドで読み取られた後に実行される、以下のコードのcreateImageメソッドのステートメントを確認できます。createImageが完了するまでdoneメソッドは実行されないと思いました。これが私のコードです:
new SwingWorker<int[], Void>() {
int result = -1;
@Override
protected int[] doInBackground() throws Exception {
// TODO Auto-generated method stub
return createImage(); //returns an integer array of size one indicating the result
}
protected void done() {
try {
result = get()[0]; //this result is being read before createImage is done
//executing. Why?
thisApplet.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
tree.setEnabled(true);
if (result == -1){
tree.setSelectionPath(null);
return;
}
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
}
}.execute();