JLabelを含むモーダルダイアログがあります。このダイアログは、長時間実行されるルックアップアクティビティを実行する別のクラスのメソッドを呼び出します。このルックアップクラスで、モーダルダイアログから渡されるJLabelを更新したいと思います。
ルックアッププロセスのコードループから、およびタイマー内からJLabelを更新しようとしました。(モーダルダイアログの実行中は、SwingタイマーのactionPerformedメソッドが呼び出されないことがわかりました。)タイマーを実装してから、ルックアッププロセスを別のスレッドに移動しました。
ルックアップは機能しますが、プロセスが終了するまでJLabelが再描画されることはありません。どんな助けにも感謝します。
関連する方法は次のとおりです。DictionaryTupleは、リストと文字列の2つのフィールドを含むPOJOです。ゲッターとセッターだけですが、dictionaryThreadはtupleListにアクセスできます。dictionaryThreadが終了するまで、ここでは触れないことに注意してください。
public List<DictionaryTuple> findTuples() {
tupleList = new ArrayList<DictionaryTuple>();
Thread dictionaryThread = new Thread(this); // This is the long-running lookup thread
dictionaryThread.start();
progressTimer = new Timer();
progressTimer.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
// progressCaption is updated in dictionaryThread
synchronized (progressCaption) {
lblProgress.setText(progressCaption);
}
lblProgress.repaint();
Thread.yield();
}
}, 250, 250);
try {
dictionaryThread.join();
} catch (InterruptedException e) {
} finally {
progressTimer.cancel();
lblProgress.setText("");
progressCaption = "";
}
return tupleList;
}
私もこのバリエーションを試しました。その場合、内部のrun()呼び出しは、処理が完了するまで保持されます。
progressTimer.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
synchronized (progressCaption) {
System.out.println("Fire!");
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
System.out.println("Update");
lblProgress.setText(progressCaption);
lblProgress.repaint();
}});
}
}
}, 250, 250);