My Java application has multiple frames. Some of them are set to be always on top. However, when the user opens another program (let's say a web browser), I want the always-on-top frames to move to the background, making way for the other application to fully show on screen.
-
2I'd love to see if anyone answers this.... – Thihara Jun 20 '12 at 11:33
-
*"My Java application has multiple frames."* See [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/a/9554657/418556) *"Some of them are set to be always on top."* Use a dialog. Not quite the effect you describe, but the path of least surprise for the user. – Andrew Thompson Jun 20 '12 at 11:53
-
@AndrewThompson This is not necessarily true - it depends on the application. I agree that it's not the best choice for a programmer jumping into a new application without specifications for such things. But there are no doubt cases where this type of behavior would be the preferred design. – Erick Robertson Jun 20 '12 at 12:02
-
@Erick Robertson [rest is here](http://stackoverflow.com/questions/6309407/remove-top-level-container-on-runtime) – mKorbel Jun 20 '12 at 12:27
2 Answers
Create your own Window Manager.
Create a custom Window Manager that implements WindowListener
, WindowStateListener
, and WindowFocusListener
. Register all new frames with this manager and use it to bring your always-on-top frames back to the front whenever the user interacts with the frames.
It sounds like your application is using some pretty custom frame management code. My guess is that as you continue developing the application, this Window Manager will take on more functionality to manage the user interface. This will not only give you the design to solve your problem, but also a foundation for any changes or enhancements to this behavior.
Please bear in mind, though, that Java cannot control what the operating system does with other applications. It can't even reliably bring a frame to the foreground above other applications in all operating systems. You will need to work with the operating systems you will support to write this Window Manager to give you the behavior that you desire.

- 2,058
- 2
- 25
- 38

- 32,125
- 13
- 69
- 98
-
How will this manage windows not instantiated from within the current JVM? – Dave Jun 20 '12 at 12:02
-
It cannot. This design will provide the foundation for controlling this behavior. It might very well be a better solution to just have one MDI-like frame and have all of the applications frames open within. This would give absolute control over such things, but I do not know the requirements and can only go off what the OP asked in the question. – Erick Robertson Jun 20 '12 at 12:07
My Java application has multiple frames. Some of them are set to be always on top.
don't do that, use CardLayout instead, in the case that you really needed end_user action then to use
Modal JDialog (or
ModalityType
withAplicationModal
)re_use JDialog (getContentPane.removeAll()) for next usage of
However, when the user opens another program (let's say a web browser), I want the always-on-top frames to move to the background, making way for the other application to fully show on screen.
this could be very annoying for end_user, use FullScreen instead
notice most important rest is in the comment by @Andrew Thompson

- 109,525
- 20
- 134
- 319