views:

78

answers:

2

I'm just experimenting with JFrames and would like to know if my application window is fully visible or obscured by some other application window. The other application window can be a native app. If this is possible, can I retrieve the size and position of the area that is not visible?

A: 

No it is not possible, since your Java application knows only its window contents, and nothing else. The window decorations and the rest of the screen are managed by the operating system. In a Unix like OS such as Linux the window manager is even a completely different user-space process. The other native app also will run in another user-space process as well.

May I ask why you want this? If you are asking about refresh/repaint issues, then there is nothing you need to do. Your OS is hopefully smart enough and it will ask your application to repaint itself when it becomes visible again.

kazanaki
I were thinking of layering information above, z-order, a specific application window. My app would be of the same size and partially transparent so the tracked window is still visible. If the tracked window request focus my window will be totally obscured and then I could call toFront() to show it again. But if some other application only obscures parts of my window I know that this is not the tracked window who has requested focus.My initial thought was to use RepaintManager and getDirtyRegion() when my window was deactivated, lost focus. But I don't think that works either!?
kolten
Use a GlassPane for this instead - http://java.sun.com/docs/books/tutorial/uiswing/components/rootpane.html
Nate
Hi Nate! Could you elaborate on this a little bit? I looked at the documentation but could not see how to use this the way I wanted.
kolten
+1  A: 

Here is a really nasty approach, which is the only approach I can think of (and wouldn't recommend):

  • Set your JFrame's glass pane to be completely red and show the glass pane (temporarily).
  • Use the Robot class to sample all pixels (or a number of pixels) from the screen coordinates where your JFrame is currently positioned.
  • If all of your samples (or most samples according to some threshold) are red then it is likely that nothing is in front of your JFrame.
  • Finally, hide the glass pane again.

Alternative (nicer solution)

Simply call toFront() on your JFrame to bring it to the front and ensure it has focus.

Adamski