How do I keep a frame always on top within the application but put it to background when using other applications in Java?
Asked Answered
C

2

9

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.

Castalia answered 20/6, 2012 at 11:30 Comment(4)
I'd love to see if anyone answers this....Delphinedelphinia
"My Java application has multiple frames." See The Use of Multiple JFrames, Good/Bad Practice? "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.Barrick
@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.Kafir
@Erick Robertson rest is hereQuotable
K
4

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.

Kafir answered 20/6, 2012 at 12:0 Comment(2)
How will this manage windows not instantiated from within the current JVM?Herpetology
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.Kafir
Q
1

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

  • JOptionPane

  • Modal JDialog (or ModalityType with AplicationModal)

  • 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

Quotable answered 20/6, 2012 at 12:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.