I have a JDialog panel, that pops up before the program starts some
long calculations. In the Dialog, the user has the option to abort the
calculations, if he doesn't have the patience to wait. Problem is,
while the he dialog window pops up in time, its content (cancel
button) doesn't get drawn until after the computer is done with the
calculations.
I've made the JDialog a thread, and the thread.join() command forces
the program to wait until that thread is fihished. It seems that this
all works. Even so, the content of the JDialog doesn't appear until
much later (too late). Despite pack() and show() commands in the
Dialog code.
Any ideas, how I can force the jdialog-content to be drawn
immediately? Someone else has posted this problem before, but I didn't
really understand the answers there, also, one of the suggestions was
using threads, which is just what I'm doing.
thanks for your help!
Andrey Kuznetsov - 12 Apr 2005 22:04 GMT
> I've made the JDialog a thread, and the thread.join() command forces
> the program to wait until that thread is fihished. It seems that this
> all works. Even so, the content of the JDialog doesn't appear until
> much later (too late). Despite pack() and show() commands in the
> Dialog code.
not JDialog should be "a thread", your long computations shold be in
another Thread.
Or you can use foxtrot.

Signature
Andrey Kuznetsov
http://uio.imagero.com Unified I/O for Java
http://reader.imagero.com Java image reader
http://jgui.imagero.com Java GUI components and utilities
Nigel Wade - 13 Apr 2005 10:34 GMT
> I have a JDialog panel, that pops up before the program starts some
> long calculations. In the Dialog, the user has the option to abort the
[quoted text clipped - 8 lines]
> much later (too late). Despite pack() and show() commands in the
> Dialog code.
You've entirely defeated the your desired purpose. When you invoke methods
on Swing objects which modify the GUI the GUI isn't updated in that method,
the method queues requests to the event dispatching thread which will be
acted on by the EDT at some convenient time later. If these calls are made
in the EDT then there's no problem, they will get acted on. However, by
placing these calls in your own thread you have broken this direct
relationship and you can't guarantee when the EDT will get around to acting
on them, if ever. To further compound this problem you've made the EDT wait
until your thread finishes before it can do anything thus actually ensuring
that it *can't* update the GUI until your thread finishes. Also, since the
EDT is blocked, the cancel button wouldn't be responsive even if it was
displayed.
> Any ideas, how I can force the jdialog-content to be drawn
> immediately? Someone else has posted this problem before, but I didn't
> really understand the answers there, also, one of the suggestions was
> using threads, which is just what I'm doing.
Yes, but you need to use threads in the correct way.
How you do this depends on your design. The simplest way would be to have
the GUI modified in the EDT before it starts your calculation thread. If
you want your calculation thread to modify the GUI then you have to pass
off the GUI modification code to the EDT by using
SwingUtilities.invokeLater() or SwingUtilities.invokeAndWait(). That way
the EDT gets notified of the updates you want and will do them when it can.
Meanwhile your calculation thread can get on with its business. How you
implement the "cancel" operation is another matter entirely...

Signature
Nigel Wade, System Administrator, Space Plasma Physics Group,
University of Leicester, Leicester, LE1 7RH, UK
E-mail : nmw@ion.le.ac.uk
Phone : +44 (0)116 2523548, Fax : +44 (0)116 2523555