Application Activate and Deactivate
Is there any way to get reliable notification when a user switches from my Swing application to another desktop application (and when they switch back)?
I couldn't find any specific event or hook to get this notification. I tried watching for WINDOW_DEACTIVATED events and triggering when a window is deactivated without a corresponding opposite window activation, but this turns out to have holes in it for some reason (sometimes switching to another desktop app can cause a WINDOW_ACTIVATED event if, for instance, a popup menu window was up).
What I'm trying to do is stop my setAlwaysOnTop windows from being bad desktop citizens, by hiding them when my app is deactivated. Apple's Cocoa had a nice "Hide on Deactivate" setting for windows.
# 1
Don't use setAlwaysOnTop, and your window will hide when your app is deactivated...
# 2
Unfortunately, floating palettes/inspectors are an integral part of every major page layout program - I really feel I need them: <http://reportmill.com>.
However, I ran a quick test with setAlwaysOnTop turned off and found those windows did not automatically hide. Also, I think application deactivation notification might come in handy for other uses as well.
# 3
> Unfortunately, floating palettes/inspectors are an integral part of every major page layout programYou should be using JDialogs as the child windows and specify the JFrame as the owner when the dialog is created.
# 4
I did originally try this approach, but it doesn't work well for multiple documents, since the dialog parent can only be specified when the dialog is created. I even had a work-around that would recreate the dialog (but not its content pane) whenever a new document window gained focus as poor substitute for the missing setParent() method.
I had assumed that SetAlwaysOnTop was introduced as a better solution for the child dialog hack.
# 5
How is JDialog related to multiple documents?I fail to see why you would need to use setParent to a JDialog.
# 6
Our page layout app <http://reportmill.com> has two floating windows - a color/font palette window and a property inspector. Both of these windows, if open, apply to the current active document window. We no longer use the dialog parent-child-owner stuff. That approach was more problematic than setAlwaysOnTop - I only mentioned it because the previous poster suggested it.
Our palette/inspector model is not dissimilar to the Adobe graphics design apps (or many apps on Mac OS X for that matter). We'd just like those windows to disappear when the app is deactivated.
# 7
> I did originally try this approach, but it doesn't work well for multiple documents
I don't understand your concept of "multiple documents". For example using the standard Swing design of using JInternalFrames within a JDesktopPane you still only have a single JFrame in which case it is no problem to specify the parent JFrame when you create the JDialog. If you need to update the properties of the dialog when a different internal frame is activated then I don't see why this is such a big problem.
> I even had a work-around that would recreate the dialog (but not its
> content pane) whenever a new document window gained focus as poor substitute for the missing setParent() method.
I don't understand this approach. I would think it would be the other way. You have a single dialog and you would change the content pane every time a new document is activated. Maybe something like a CardLayout in your dialog would work. So you just swap in the propery panel of the current document.
Even if you hide the dialog of the previous document and create/show a new dialog for the current document I don't see what the problem is because you only ever have a single owner JFrame.
If your implementation of "multiple documents" is acutally multiple JFrames, then you should say so. Even in this situation I don't see the problem since each document would map to a single JFrame, so you would always know the owner.
Basically, I don't understand your problem or question. Why do you feel you continually need to change the owner of the dialog?
# 8
I don't really want to talk about JDialog anymore. Does anyone know how to get application activate and deactivate notifications?
# 9
> I don't really want to talk about JDialog anymore.
If we don't understand the problem, how are we supposed to help with the solution.
> Does anyone know how to get application activate and deactivate notifications
You already know how as you stated in you initial question.
# 10
If you're trying to do something that no one was able to successfully implement until now, that should be a big hint that your logic is wrong.
I developed more than one application that have palettes similar to photoshop idea.
There are 2 options for that:
1. Use JInternalFrame with layer set to palette.
2. Use JDialog with parent set to JFrame.
Both work great.
If you have a special case, explain in more details what your appilcation is exactly doing.
Trying to play around with setAlwaysOnTop is for sure the wrong approach.