progress dialog

C

cell

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.

Could anyone tell me some hints to solve it?

Thanks!
 
R

RedGrittyBrick

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.
 
A

Andrew Thompson

cell wrote:
...
Could anyone tell me some hints to solve it?

Prepare an SSCCE*. If the solution does not become obvious
while preparing it, post the SSCCE here for further investigation.

Note that in order to take 'a lot of time' to prepare the second
dialog, it might be necessary to throw in some arbitrary 'work'
for the simpler version (e.g. calculate the average value of a million
random numbers).

* <http://www.physci.org/codes/sscce.html>

--
Andrew Thompson
http://www.physci.org/

Message posted via JavaKB.com
http://www.javakb.com/Uwe/Forums.aspx/java-general/200711/1
 
D

Daniel Pitts

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.

Could anyone tell me some hints to solve it?

Thanks!
From your description, it sounds like your "collect data" process is
happening on the Event Dispatch Thread. You would be better off passing
that work into a new thread (see SwingWorker), and when that work is
finished, firing off an event that opens your new dialog.

I suggest reading more about Swing and Threads. Google for that term.
I also suggest reading the book Java Concurrency in Practice. It
explains a lot of the subtleties of working in Java when dealing with
multiple threads. Most modern Java apps are multithreaded, whether the
authors know it or not.

More info on Java Concurrency in Practice:
<http://virtualinfinity.net/wordpress/technical-book-recommendations/java-concurrency-in-practice/>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,072
Latest member
trafficcone

Latest Threads

Top