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?
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.
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 yourJFrame
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.