Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 889
Pantaziu Cristian Avatar asked Jun 20 '12 11:06

Pantaziu Cristian


People also ask

How do you make a JFrame always on top?

How do I set a JFrame always on top? After you create another window, call toFront() on your JFrame that you want to be at the front. myFrame. setAlwaysOnTop(true);

Which method is used to show the frame window?

A JFrame is like a Window with border, title, and buttons. We can implement most of the java swing applications using JFrame. By default, a JFrame can be displayed at the top-left position of a screen. We can display the center position of JFrame using the setLocationRelativeTo() method of Window class.

What type of frame we are using in creating Java GUI?

JFrame class is a type of container which inherits the java. awt. Frame class. JFrame works like the main window where components like labels, buttons, textfields are added to create a GUI.


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.

like image 101
Erick Robertson Avatar answered Oct 07 '22 11:10

Erick Robertson


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

like image 37
mKorbel Avatar answered Oct 07 '22 11:10

mKorbel