JDialog dispose and application window popping  
Author Message
aclarke





PostPosted: 2004-9-21 4:03:00 Top

java-programmer, JDialog dispose and application window popping Hello!

I have an application where I show a JDialog (sort of as a status
indicator). During this time, I launch an application like Excel
(which comes up successfully). Once that other application is
launched, I take down my dialog by calling dlg.dispose(). Note that
the user did NOT interact with that dialog.

After the dialog disappears, my application pops itself to be
frontmost. I cannot figure out how to make my application stop this
behavior. I want the second application (Excel in this case), to
remain in front.

I have been searching all over and cannot find any solution to this
problem. I do know its tied up with the dispose() because not invoking
it leaves things the way I want (except that my modal dialog is still
up).

Any pointers would be appreciated.

Allan
 
Paul Lutus





PostPosted: 2004-9-21 12:55:00 Top

java-programmer >> JDialog dispose and application window popping ac wrote:

/ ...

> I have been searching all over and cannot find any solution to this
> problem. I do know its tied up with the dispose() because not invoking
> it leaves things the way I want (except that my modal dialog is still
> up).

The simple version: Java can only control the Z-order of its own windows. It
cannot control the order of system windows.

--
Paul Lutus
http://www.arachnoid.com

 
aclarke





PostPosted: 2004-9-21 20:32:00 Top

java-programmer >> JDialog dispose and application window popping Paul Lutus <email***@***.com> wrote in message news:<email***@***.com>...
> ac wrote:
>
> / ...
>
> > I have been searching all over and cannot find any solution to this
> > problem. I do know its tied up with the dispose() because not invoking
> > it leaves things the way I want (except that my modal dialog is still
> > up).
>
> The simple version: Java can only control the Z-order of its own windows. It
> cannot control the order of system windows.

Understood. The behavior I'm seeing is that my Java app is popping
itself to the front. Here is the time order of events:

1. launch my app (the frontmost app window)
2. put up modal dialog in my app
3. launch Excel (which is now the frontmost app window)
4. programmatically dispose of the modal dialog
5. my app moves itself to be frontmost

That last event is the problem and is some default behavior in the
JDK.

Ideas? Hints? Imprecations?

Allan
 
 
Paul Lutus





PostPosted: 2004-9-22 3:04:00 Top

java-programmer >> JDialog dispose and application window popping ac wrote:

> Paul Lutus <email***@***.com> wrote in message
> news:<email***@***.com>...
>> ac wrote:
>>
>> / ...
>>
>> > I have been searching all over and cannot find any solution to this
>> > problem. I do know its tied up with the dispose() because not invoking
>> > it leaves things the way I want (except that my modal dialog is still
>> > up).
>>
>> The simple version: Java can only control the Z-order of its own windows.
>> It cannot control the order of system windows.
>
> Understood.

Evidently not.

> The behavior I'm seeing is that my Java app is popping
> itself to the front. Here is the time order of events:
>
> 1. launch my app (the frontmost app window)
> 2. put up modal dialog in my app
> 3. launch Excel (which is now the frontmost app window)
> 4. programmatically dispose of the modal dialog
> 5. my app moves itself to be frontmost
>
> That last event is the problem and is some default behavior in the
> JDK.

No, the JDK (actually, the JRE) has nothing to do with this behavior. I
repeat:

>> The simple version: Java can only control the Z-order of its own windows.
>> It cannot control the order of system windows.

Excel is not a Java application, it is a system application. It is your OS
that is choosing to place the Java application in front of the Excel
application. This has nothing to do with Java.

--
Paul Lutus
http://www.arachnoid.com

 
 
Larry Barowski





PostPosted: 2004-9-22 11:07:00 Top

java-programmer >> JDialog dispose and application window popping
"Paul Lutus" <email***@***.com> wrote in message
news:email***@***.com...
> Excel is not a Java application, it is a system application. It is your OS
> that is choosing to place the Java application in front of the Excel
> application. This has nothing to do with Java.

So there is no way to programmatically close a dialog in Windows
without popping the parent window to the front? If there is a
way, then the Java library code should use it. It makes no sense
to move a window to the front just because a child dialog was
closed.


 
 
Paul Lutus





PostPosted: 2004-9-22 13:03:00 Top

java-programmer >> JDialog dispose and application window popping "Larry Barowski" <larrybarATengDOTauburnDOTeduANDthatISall> wrote:

>
> "Paul Lutus" <email***@***.com> wrote in message
> news:email***@***.com...
>> Excel is not a Java application, it is a system application. It is your
>> OS that is choosing to place the Java application in front of the Excel
>> application. This has nothing to do with Java.
>
> So there is no way to programmatically close a dialog in Windows
> without popping the parent window to the front? If there is a
> way, then the Java library code should use it. It makes no sense
> to move a window to the front just because a child dialog was
> closed.

The problem is not that the window is moved to the front -- as far as Java
is concerned, there were two windows, now there is one. Obviously it is in
front.

As far as the system is concerned, the window is in front of the one that
contains Excel, for the simple reason that the dialog has just had the
focus, so the focus shifts to the dialog's parent window. But this is a
system issue, it has nothing to do with Java. In fact, any application that
has a modal dialog that is closed will receive the focus.

--
Paul Lutus
http://www.arachnoid.com

 
 
Babu Kalakrishnan





PostPosted: 2004-9-22 17:33:00 Top

java-programmer >> JDialog dispose and application window popping Paul Lutus wrote:
>
> The problem is not that the window is moved to the front -- as far as Java
> is concerned, there were two windows, now there is one. Obviously it is in
> front.
>
> As far as the system is concerned, the window is in front of the one that
> contains Excel, for the simple reason that the dialog has just had the
> focus, so the focus shifts to the dialog's parent window. But this is a
> system issue, it has nothing to do with Java. In fact, any application that
> has a modal dialog that is closed will receive the focus.
>

I disagree with this point of view. If the system Window Manager is
bringing the Java window into focus, it is certainly because it received
some event that caused it to come to front - and that event couldn't
have originated from anywhere other than the JVM.

I would suggest that the OP look in Sun's bug database to see if this
has been filed as a bug, and if not file one. It most probably is an AWT
implementation bug - I suspect the AWT must be signalling the OS
Windowing system for the main frame to be brought into focus, and this
is wrong behaviour. The signal must be sent only if the dialog that was
closing was in focus at the time of its closing and not otherwise (And I
don't think it is impossible for the AWT subsystem to determine if the
dialog was the system focused window or not)

BK
 
 
aclarke





PostPosted: 2004-9-22 20:31:00 Top

java-programmer >> JDialog dispose and application window popping "Larry Barowski" <larrybarATengDOTauburnDOTeduANDthatISall> wrote in message news:<email***@***.com>...
> "Paul Lutus" <email***@***.com> wrote in message
> news:email***@***.com...
> > Excel is not a Java application, it is a system application. It is your OS
> > that is choosing to place the Java application in front of the Excel
> > application. This has nothing to do with Java.
>
> So there is no way to programmatically close a dialog in Windows
> without popping the parent window to the front? If there is a
> way, then the Java library code should use it. It makes no sense
> to move a window to the front just because a child dialog was
> closed.

Paul, I am not convinced that this behavior is being implemented in
the OS at all. I have MFC-based applications which do not do this. I
have been digging around some in the JDK source and I think it has
something to do with the focus cycle root.

Another hint: if I just hide the modal dialog, my application still
pops forward in z-order.

I do appreciate the comments/advice.
 
 
Larry Barowski





PostPosted: 2004-9-23 6:41:00 Top

java-programmer >> JDialog dispose and application window popping
"Paul Lutus" <email***@***.com> wrote in message
news:email***@***.com...
> "Larry Barowski" <larrybarATengDOTauburnDOTeduANDthatISall> wrote:
>
> The problem is not that the window is moved to the front -- as far as Java
> is concerned, there were two windows, now there is one. Obviously it is in
> front.

Read the original post again. There were three windows,
the Excel window, the main window for the Java
application, and the dialog for the Java application. The
Excel window was on top. Closing the dialog
programmatically caused the main window for the Java
app to move to the front. I'm posting a work-around in
response to the original post.


 
 
Larry Barowski





PostPosted: 2004-9-23 6:48:00 Top

java-programmer >> JDialog dispose and application window popping
"ac" <email***@***.com> wrote in message
news:email***@***.com...
> Hello!
>
> I have an application where I show a JDialog (sort of as a status
> indicator). During this time, I launch an application like Excel
> (which comes up successfully). Once that other application is
> launched, I take down my dialog by calling dlg.dispose(). Note that
> the user did NOT interact with that dialog.
>
> After the dialog disappears, my application pops itself to be
> frontmost.

It seems this only happens for modal dialogs. Using
setModal(false) then disposing immediately still
causes the problem, but setModal(false) followed
by a dispose() in an invokeLater() seems to work,
like this:

dialog.setModal(false);
SwingUtilities.invokeLater(
new Runnable() {
public void run() {
dialog.dispose(); } } );

I tested this on 1.3.1_04 and 1.5.0 rc.


 
 
aclarke





PostPosted: 2004-9-23 20:27:00 Top

java-programmer >> JDialog dispose and application window popping "Larry Barowski" <larrybarATengDOTauburnDOTeduANDthatISall> wrote in message news:<email***@***.com>...
> "ac" <email***@***.com> wrote in message
> news:email***@***.com...
> > Hello!
> >
> > I have an application where I show a JDialog (sort of as a status
> > indicator). During this time, I launch an application like Excel
> > (which comes up successfully). Once that other application is
> > launched, I take down my dialog by calling dlg.dispose(). Note that
> > the user did NOT interact with that dialog.
> >
> > After the dialog disappears, my application pops itself to be
> > frontmost.
>
> It seems this only happens for modal dialogs. Using
> setModal(false) then disposing immediately still
> causes the problem, but setModal(false) followed
> by a dispose() in an invokeLater() seems to work,
> like this:
>
> dialog.setModal(false);
> SwingUtilities.invokeLater(
> new Runnable() {
> public void run() {
> dialog.dispose(); } } );
>
> I tested this on 1.3.1_04 and 1.5.0 rc.

Paul, I think you misunderstood my description of the window
placement. After all windows were visible, the Excel window was in
front, the modal dialog was next, then my app's window was next. It
was not the ordering: modal dialog, excel, my app.

Larry, many thanks for the workaround. I will go give it a try.

Thanks to all who took an interest in this thread.

Allan
 
 
Babu Kalakrishnan





PostPosted: 2004-9-23 20:40:00 Top

java-programmer >> JDialog dispose and application window popping ac wrote:
>
> Paul, I think you misunderstood my description of the window
> placement. After all windows were visible, the Excel window was in
> front, the modal dialog was next, then my app's window was next. It
> was not the ordering: modal dialog, excel, my app.
>
> Larry, many thanks for the workaround. I will go give it a try.
>
> Thanks to all who took an interest in this thread.
>

If you have a small piece of code which will reproduce this symptom
everytime, please do file a bug report at Sun's site attaching the
sample code. I couldn't locate anything in their bug database database
with similar symptoms (most of them are related to windows *not* coming
to front !!) Might help them fix it for the next version of the JDK at
least..

BK
 
 
Larry Barowski





PostPosted: 2004-9-24 1:52:00 Top

java-programmer >> JDialog dispose and application window popping
"Babu Kalakrishnan" <email***@***.com> wrote in message
news:email***@***.com...
> ac wrote:
> If you have a small piece of code which will reproduce this symptom
> everytime, please do file a bug report at Sun's site attaching the
> sample code. I couldn't locate anything in their bug database database
> with similar symptoms (most of them are related to windows *not* coming
> to front !!) Might help them fix it for the next version of the JDK at
> least..

I've already done this, so Allan need not bother.


 
 
Larry Barowski





PostPosted: 2004-9-24 3:15:00 Top

java-programmer >> JDialog dispose and application window popping > dialog.setModal(false);
> SwingUtilities.invokeLater(
> new Runnable() {
> public void run() {
> dialog.dispose(); } } );

This doesn't seem to work in most situations. You can hide
the dialog without popping up a frame, by doing:

dialog.setModal(false);
dialog.setVisible(false);

So you could do this, then wait to dispose the dialog until
another one is popped up, or just keep the dialog around
and re-use it (be sure to set it modal again before re-using).

Also, I find that the popped up window need not be the
parent of the dialog. If the parent is not visible, some other
frame (if there is one) will pop up.