Search code examples
javaswingconcurrencyjlabelevent-dispatch-thread

Trying To Update the JLabel in a Modal Dialog


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);

Solution

  • 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:

    • Always update the JLabel on the Swing event thread or EDT (event dispatch thread).
    • Don't use a java.util.Timer to update the components of a Swing app.
    • You can use a javax.swing.Timer to update Swing components, but that's not going to work here, unless you are using it to poll the background process.
    • Consider using a SwingWorker for your background threading and then update the JLabel via eitherthe publish/process methods of the worker or via a PropertyChangeListener added to the SwingWorker.
    • Call all your methods that you want running when the dialog is running before setting the dialog to visible.