This week's book giveaway is in the OCAJP forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide 1Z0-808 and have Jeanne Boyarsky & Scott Selikoff on-line! See this thread for details.
The application has a command to upload bunch of files to a server over a ssh link. So I have the transfer done in a SwingWorker class, which fires property change events. I have a property change listener in a JDialog class that is supposed to update a progress bar and some text fields like which file it's on, how many bytes have been sent, transfer rate. Basically enough fluff to keep the user entertained during the transfer.
That part works fine as a println in the property change listener shows it is receiving regular (and correct) updates.
My problem is that I can't figure out how to get the dialog to draw all the items when they are updated.
I can get the progress bar to work with:
Although it does flash a little, it's acceptable.
If I try that with a JLabel it writes the new value over the old without clearing so it's soon unreadable.
I've tried, repaint(), paint(Graphics g), update (Graphics g). Paint and repaint don't seem to do anything.
Of course once the transfer finishes the dialog is repainted with the final values just fine.
It must be a simple call I'm missing. I don't know why I seem to always get the hard part working and spend hours on the simplest things.
Any help is appreciated.
It's not what your program can do, it's what your users do with the program.
Never call getGraphics() of a Component in client code.
Never invoke update(...) or any painting methods (other than repaint())directly, and never override the update() method for a Swing component.
As far as your problem is concerned, you really need to go through the tutorial on Concurrency in Swing. And didn't you read the API for JProgressBar? There's a link there to the tutorial on How to Monitor Progress which would have provided adequate guidance.
There are no new questions, but there may be new answers.
I have read the Concurrency in Swing tutorial and the API and tutorials on progress bars. I thought I understood them but I'm obviously missing something. I have progress bars and updating tables and text fields working in other programs with repaint.
My problem is calling repaint() leaves me with a blank dialog.
I think it is time to write a simple demo program and see if I can find the underlying flaw in my logic.