cell said:
Dear all,
I want to open a progress dialog (modal is false) to remind the user
to wait and then open another dialog A. The dialog A needs about 20
seconds to open as it needs to collect data. However, I find that the
progress dialog open but cannot show its content in the dialog.I have
tried to write a class which extends Thread class and repaint the
progress dialog about 0.5sec inside it. However, it still cannot
repaint during opening dialog A.
When you say "repaint" it sounds to me that you are messing around at
too low a level. It sounds like your problem is you are blocking the
EDT. You just need to arrange threads appropriately and all will work fine.
Could anyone tell me some hints to solve it?
Your description is a bit confusing to me but if I understand it
correctly, the approach I have used is for dialog A to extend Observable
(or to receive an Observable during construction). The progress dialog
implements Observer and it's update() method is used to update a
progress bar or some other indicator.
I'd have Dialog A display itself empty and start a new (i.e. non EDT)
thread to go and retrieve it's data. As this thread progresses it would
call notifyObservers() (or update the passed Observable). Along the way
(or at the end) it would update A's contents (using
SwingUtilities.invokeLater() to ensure GUI work happens on the EDT).
I'm simplifying somewhat since I'd separate model from view.
If you update the GUI too frequently it will slow things down so I find
it best to throttle GUI updates of this sort to once every 250 ms (for
example). When adding rows to a JTable, I don't call
fireTableRowsInserted() for every row, I check elapsed time since last
update.
Because notifyObservers in A is called from a thread other than the EDT,
the update() method should take care to use Swingutilities.invokeLater()
to perform any Swing methods such as updating a JProgressBar.
Using threads is tricky. I am not an expert, I am hoping the real
experts will correct any blunders in the above.
If you put together a small SSCCE this could be clarified more easily.
http://mindprod.com/jgloss/sscce.html
http://homepage1.nifty.com/algafield/sscce.html
P.S. As of Java 1.3, SwingUtilities.invokeLater is just a cover for
java.awt.EventQueue.invokeLater(). You should probably use the latter.