
Signature
Andrei Kouznetsov
http://uio.dev.java.net Unified I/O for Java
http://reader.imagero.com Java image reader
http://jgui.imagero.com Java GUI components and utilities
> > I have a JDialog that has a button that displays a second JDialog over
> > top the first. The OK button on the second dialog calls
[quoted text clipped - 4 lines]
>
> start it in another thread or with SwingUtilities.invokeLater(Runnable)
That will not work. The problem is that the long process is running in
the event dispatch thread. SwingUtilities.invokeLater() will queue the
Runnable to be run later, but still in the event dispatch thread.
The long process needs to be run in a different thread. You could "roll
your own", but it would be better to use the SwingWorker class, which can
be found in the Java Tutorial on the Sun web site.
You also probably want to provide some sort of UI while the long process
is running, which would show the progress of the process and would give
the user the option of canceling the process. A typical way to do this
would be to show a dialog containing a JProgressBar and a "Cancel" button.
If the user should not be allowed to perform other actions while the
process is executing, you can make this dialog modal, otherwise use a
non-modal dialog.

Signature
Regards,
John McGrath
Bruce Stemplewski - 22 Nov 2004 01:28 GMT
Thanks what I don't understand is that I close the second dialog box
before I start the long process. Why isn't the first dialog repainted?
Is it because the button event from the first dialog has not been exited?
>>>I have a JDialog that has a button that displays a second JDialog over
>>>top the first. The OK button on the second dialog calls
[quoted text clipped - 20 lines]
> process is executing, you can make this dialog modal, otherwise use a
> non-modal dialog.
Babu Kalakrishnan - 22 Nov 2004 05:35 GMT
Please do not top post.
> Thanks what I don't understand is that I close the second dialog box
> before I start the long process. Why isn't the first dialog repainted?
> Is it because the button event from the first dialog has not been exited?
You could say that. ActionEvents as well as painting occur in the same
thread - i.e the EventDispatchThread. When you close the dialog, it
doesn't go and automatically repaint all the regions first before
continuing with the statement after the setVisible(false) (or
dispose()), but just queues a repaint on the area just needs repainting.
The actual repainting will be done only at the time that it processes
all paint events (this allows it to combine multiple repaint requests
which may have occurred on the component into a single paint).
If you block the EDT by performing a time consuming task, the paint can
obviously take place only after you have finished the task : the reason
why you see the "shadow". The important thing to remember when workin
gunder AWT/Swing is that you should never perform any time consuming
tasks under the EDT, and should do these in a separate thread.
BK
John McGrath - 22 Nov 2004 06:32 GMT
> Thanks what I don't understand is that I close the second dialog box
> before I start the long process. Why isn't the first dialog repainted?
> Is it because the button event from the first dialog has not been exited?
Yes, exactly. No events are processed while your process is running.
Events are processed / dispatched by the event dispatch thread, which runs
a simple loop that just pulls an event from the event queue and dispatches
it. Paint events are placed on this queue, and they are dispatched by
calling your component's paint() method. Action events created when one
of your component's buttons are pushed are also placed on this queue, and
they are dispatched by calling your listener's actionPerformed() method.
The obvious implication of this is that no painting will occur until your
actionPerformed() method returns.
Modal dialogs are different - basically what happens there is that, when a
modal dialog is shown, it starts a new event loop, inside the setVisible()
method. That is why you do not need to return from your actionPerformed()
method before the modal dialog can be painted.
There is some very good material covering this in The Java Tutorial on the
Sun web site (http://java.sun.com/docs/books/tutorial/index.html). I
would definitely recommend that you take a look at this, especially the
trails, "Essential Java Classes" and "Creating a GUI with JFC/Swing".

Signature
Regards,
John McGrath
Andrei Kouznetsov - 22 Nov 2004 07:09 GMT
>> > I have a JDialog that has a button that displays a second JDialog over
>> > top the first. The OK button on the second dialog calls
[quoted text clipped - 8 lines]
> the event dispatch thread. SwingUtilities.invokeLater() will queue the
> Runnable to be run later, but still in the event dispatch thread.
It is _better_ to start long process in separat thread,
however it will _work_ also
with invokeLater because long process started
_after_ dialog was closed and repainting was made.

Signature
Andrei Kouznetsov
http://uio.dev.java.net Unified I/O for Java
http://reader.imagero.com Java image reader
http://jgui.imagero.com Java GUI components and utilities
John McGrath - 22 Nov 2004 08:43 GMT
> It is better to start long process in separat thread,
> however it will work also
> with invokeLater because long process started
> _after_ dialog was closed and repainting was made.
I am quite sure that will not work. Paint events have a low priority, so
the event posted with invokeLater() will be processed before any paint
events.
Have you actually tried doing this? If you really think this will work,
would you please post a runnable example that demonstrates how it works?
If you really wanted to do something like this, you could force the dialog
to paint by calling paintImmediately() on the dialog's content pane. But
that would only deal with the initial dialog exposure. If the dialog were
covered and then exposed by some other window during the long process, it
would not repaint. The only *correct* way to handle long-running
processes under Swing/AWT is to run them in a separate thread.

Signature
Regards,
John McGrath
Bruce Stemplewski - 23 Nov 2004 04:04 GMT
Thanks to everyone for all of their help.
I am using the SwingWorker class and so far everything is working great.
Now I want to add a progress dialog.
I assume I would create this Progress dialog in the main thread?
Is it safe for the worker thread to update this progress dialog? Is it
safe for the thread to close the dialog once the work is done in the thread?
>>>I have a JDialog that has a button that displays a second JDialog over
>>>top the first. The OK button on the second dialog calls
[quoted text clipped - 20 lines]
> process is executing, you can make this dialog modal, otherwise use a
> non-modal dialog.
Thomas Weidenfeller - 23 Nov 2004 08:20 GMT
> Now I want to add a progress dialog.
>
[quoted text clipped - 3 lines]
> safe for the thread to close the dialog once the work is done in the
> thread?
May I suggest the following:
(a) You stop top-posting. The group's FAQ contains references to a
number of resources regarding proper newsgroup behavior. You might want
to check them out. Of course, you can ignore them, but this
significantly reduces your chance of getting an answer here.
(b) Please read
http://java.sun.com/docs/books/tutorial/uiswing/index.html
from the first to the last page. A newsgroup is not a good substitute
for learning a GUI toolkit. People will sooner or later get angry at you
if you don't do your homework and learn the basics.
(c) After (b), please at least browse through
http://java.sun.com/docs/books/tutorial/2d/index.html
because these issues usually come up sooner or later, too.
(d) The FAQ points to a number of TSC papers, regarding Swing's painting
mechanism and architecture. You might want to give them a quick look,
too. So in case you need background information you know where to find it.
/Thomas

Signature
ftp://ftp.cs.uu.nl/pub/NEWS.ANSWERS/computer-lang/java/gui/faq
Bruce Stemplewski - 23 Nov 2004 23:06 GMT
I am NOT top posting. At least not on purpose. My posts are showing
up in normal order on my newsreader. This newsgroup gets very little
traffic so perhaps it looks like top posting.
Thanks for the links but newsgroups are a place to ask all kinds of
questions. I never once asked anyone to teach me GUI from the ground
up. I just asked for some advice.
>> Now I want to add a progress dialog.
>>
[quoted text clipped - 30 lines]
>
> /Thomas
John McGrath - 24 Nov 2004 05:19 GMT
> I am NOT top posting.
Yes, you are. Top posting is the practice of placing your response at the
top of the message and blindly quoting the entire message that you are
responding to at the bottom. This is generally considered to be very bad
form.
Here is a good resource on quoting style and other posting-related issues:
http://www.allmyfaqs.com/faq.pl?How_to_post

Signature
Regards,
John McGrath
Bruce Stemplewski - 24 Nov 2004 22:40 GMT
> Yes, you are. Top posting is the practice of placing your response at the
> top of the message and blindly quoting the entire message that you are
[quoted text clipped - 4 lines]
>
> http://www.allmyfaqs.com/faq.pl?How_to_post
Sorry I have always know top posting as changing the date so your post
appears at the top of all the messages.
I have been using news forums for years and this is the first time I
have ever heard any one complain about quoting options.
Chris Smith - 24 Nov 2004 22:52 GMT
> Sorry I have always know top posting as changing the date so your post
> appears at the top of all the messages.
>
> I have been using news forums for years and this is the first time I
> have ever heard any one complain about quoting options.
That's amazing! Not being sarcastic... really, it is. You've
apparently been participating in USENET since 1996, and never heard the
definition of top-posting?

Signature
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.
Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
Bruce Stemplewski - 23 Nov 2004 23:25 GMT
My last post shows as Tue, 23 Nov 2004 04:04:02 GMT (11:00 PM local
time = -5 GMT) on my news reader. This is exactly when I posted it.
May I suggest that you be careful before you accuse someone of top posting?
>> Now I want to add a progress dialog.
>>
[quoted text clipped - 30 lines]
>
> /Thomas