I have a modal dialog containing a JLabel. This dialog calls a method in another class which does a long-running lookup activity. I'd like this lookup class to update the JLabel, which is passed from the modal dialog.
I've tried updating the JLabel from the code loop in the lookup process, and from within a Timer. (I found that the actionPerformed method of a Swing timer never gets called when a modal dialog is running.) Since implementing the Timer, I moved my lookup process to a separate Thread.
My lookup works, but the JLabel is still never repainted until the end of the process. I'll be grateful for any help.
Here's the relevant method. The DictionaryTuple is a POJO containing two fields: a List and a String. Just getters and setters, but dictionaryThread does have access to tupleList; note that we don't touch it here until dictionaryThread is finished.
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;
}
I've also tried this variation; in that case, the inner run() invocations are held until the processing is completed:
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);
You need to show us exactly where you set the dialog to visible -- is it before or after you call the findTuples()
method? This is critical.
Suggestions: