Progress bar for Java step from EDT problem

This is for rocking professionals. I spent a lot of time on this problem, so I’ll need a few lines to explain the problem.

I have a separate java swing application (java 6). In my application, I have a frame with a group of radio buttons. I have one action associated with all the buttons in the group. The action checks which radio button is selected and does any work. "Work" includes some background calculations, as well as some picture in two other frames in my application. Background computing is multithreaded.

I want to display a progress bar when the user selects one of the radio buttons. However, when a radio button is selected while an action on the radio button occurs, a progress indicator never appears. I tried jdialog progress indicators, glass panel, etc. None of them appear until the "work" is completed. It looks like Swing does not finish drawing the radio button until the “work” in the corresponding action is completed. And since the EDT does only one thing at a time, the progress bar dialog (or glass) is never displayed.

Then I tried to use SwingWorker to do all this “work”. Start the progress bar (or activate the glass panel), launch SwingWorker and close the progress bar (or deactivate the glass panel) in the done () method for SwingWorker. This seems to perfectly reflect the progress, but the picture, which is part of the “work”, sometimes does not finish, leaving me with some artifacts of painting (the paintComponent method is quite complicated, so I don’t want to reproduce it here). Artifacts disappear if I resize the window. This actually happens if I use a class that extends Thread instead of SwingWorker. This is all because Swing is not thread safe, and I'm trying to make the GUI work from a thread other than EDT. I understand this part.

What should I do? "work" takes about 30 seconds, and it seems too long to not show the user any indication that the program is working. I also tried changing the cursor to a wait cursor and ran into the same problems as above. The only thing I can do is turn off the frame and set the frame title to some text, for example, "working ..."

Has anyone seen this problem before?

+6
java radio-button swingworker progressdialog
source share
2 answers

I think you are right to do the work in the SwingWorker stream, but you should not try to do your picture there.

I would be inclined:

  • Show the ActionListener () indicator in the execution line, start swingworker and exit

  • Ask the worker thread to do the work and periodically call repaint () on the progress bar component (this is guaranteed to be thread safe)

  • The progress bar has its own paintComponent (which will be automatically called on the EDT). If necessary, this can read some variable that is updated by the workflow to measure progress.

  • When the workflow ends, call invokeLater () to run the final close function on the EDT, which will hide the progress bar and do any other GUI cleanup / show a message about the end of the user, etc.

+4
source share

When you moved work from EDT to a swinging worker (which was the right thing to do), it sounds like work and painting moved to a swinging worker. The picture must still take place on the EDT. You can achieve this using SwingUtilities.invokeLater to trigger a redraw from the background thread, or using SwingWorker.publish (V ...) , which will receive notifications from your workflow and make them available via EDT using SwingWorker.process (V ...) the template method (which you will override). Your overriding process can handle interim notifications by redrawing part of the screen, updating progress, or taking some other appropriate action as desired. Any user interface changes made here will be visible without waiting for the completion of the remaining work.

+4
source share

All Articles