1

I have a Java swing program consisting of many JFrames, I know not the best design, but it does the job.

If I open another program, for example Firefox, and maximize that window, my JFrames are not visible.

Now when I click the button on the window taskbar of one of my JFrames the JFrame popups up.

When this JFrame popups up I need check if one of my other JFrames are visible, I mean are in front of Firefox window.

I tried isVisible(), isShowing() and isDisplayable(), but they always return true even when the JFrame is below the Firefox window. That makes sense when reading the Java documentation.

But I can't seem to find a method to tells me if my JFrame is really visible in front of the Firefox window.

It puzzles me.

A JFrame covered by Firefox has the following properties:

14:50:08,129 DEBUG            DrawFrame - isVisible: true
14:50:08,129 DEBUG            DrawFrame - isDisplayable: true
14:50:08,129 DEBUG            DrawFrame - isShowing: true

Just to explain my goal: I have multiple JFrames and when I click one specific JFrame button on the window taskbar I want to check if another JFrame is visible for the user. If it is not visible for the user (for example when covered by another program) than I want to make it visible. Making it visible is simple: WindowListener windowActivated and call toFront on the not visible frame, but of course this causes a loop when the other JFrame is activated again (calling windowActivated > toFront > windowActivated > toFront, and so on). That is why I need to check the visibility before calling the toFront method.

I'm not aware of a function in Java to check if a window is visible for the user, so going native may be the only solution.

TylerH
  • 20,799
  • 66
  • 75
  • 101
TinusSky
  • 1,657
  • 5
  • 24
  • 32
  • Works for me on Mac OS X using this [example](http://stackoverflow.com/a/3245805/230513). – trashgod May 13 '14 at 11:45
  • The example you are referring to doesn't illustrate any checks on how to see if a frame is visible in front of another program. See my question for more information – TinusSky May 13 '14 at 12:53
  • Please define _really_ in this context. In the dock (similar to task bar), I see a checkmark next to the frontmost frame. – trashgod May 13 '14 at 12:58

1 Answers1

0
  • use windowGainedFocus/LostFocus from WindowFocusListener

  • loop in an array of Window[] wins = Window.getWindows();, returns all Top-Level Containers in the current JVM

  • be sure that in loop by test if (wins[i] instanceof JFrame) { (or JDialog/JWindow ...)

TylerH
  • 20,799
  • 66
  • 75
  • 101
mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • I'm not fully understand what you mean, but tracking if a frame is visible using a windowfocuslistener is not correct, cause a frame can be visible (in front of another window) when i hasn't the focus, cause another frame can have the focus. – TinusSky May 13 '14 at 12:52
  • 1
    @Tinus: Don't forget that your trying to work around [known problems with multiple frames](http://stackoverflow.com/q/9554636/230513). It may help if you explain the goal. – trashgod May 13 '14 at 13:00
  • @trashgod i've edited my question, thanks! Note: i know the limitations, the only problem this little "feature" is not worth rewriting the program's gui. I'm happy with every dirty trick even going native. – TinusSky May 13 '14 at 13:21
  • @Tinus no idea without an SSCCE/MCVE, short, runnable, compilable, seems like as your issue, topic isn't about specific methods, but in code, you can to test with [another Listeners added to JFrames](http://stackoverflow.com/questions/10880326/jpanel-which-one-of-listeners-is-proper-for-visibility-is-changed), isXxx couldn't be proper notifiers, nor clear from which of the nested JFrames classes is event fired, forgot for isDisplayable, is notifier about health of this container – mKorbel May 13 '14 at 13:38