10

I'm searching for a solution in order to keep a JFrame always on top and with always I really mean always.

setAlwaysOnTop( true );

This won't work when I'm starting a game in fullscreen mode. I know you normally don't want your windows to stay on top but in this case it's required.

  • 2
    "always on top and with always I really mean always." Even if there are two of them? – Mark Byers Jun 16 '10 at 18:11
  • Well there will be just one instance of this application running. With always I just wanted to say that fullscreen applications like games shouldn't hide this window + the fullscreen window is supposed to be still visible (in the background) –  Jun 16 '10 at 18:15
  • In a multi-windowed system only one window can be on top at any given time (Highlander: In the end there can be only one). So a request to be always on top is at best just that, a *request* which can't be guaranteed. A full screen application has taken always on top to the next level - "I want to be always on top and to have full and exclusive control of the display" - it's always on top trumps your always on top. – Lawrence Dol Jun 16 '10 at 18:30

8 Answers8

4

This can't be done.

For example, the Windows Task Manager, even when set to Always on Top will get covered up by full-screen applications.

This is due to the fact that full-screen applications typically use a different graphics context and can't be overlayed.

Ben S
  • 68,394
  • 30
  • 171
  • 212
  • there's a strange behaviour on Windows 8 (if it counts) that windows are even above games if set to always on top (I'd call it a feature though not a bug) – Daniel Sharp Jul 25 '14 at 19:16
2

Start another process to check if the window is on top,if not, set it on top.

Cheng Chen
  • 42,509
  • 16
  • 113
  • 174
1

This sounds like the kind of question that Raymond Chen always has to answer over at Link. How can you really really forever and for true keep a window in the foreground? You can't. Because what happens if somebody ELSE's window uses the same trick to keep itself always always and forever in the foreground? Which one wins?

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
Jim Kiley
  • 3,632
  • 3
  • 26
  • 43
1

This is a sample code that should be helpful

public class AllWaysOnTop extends JFrame implements WindowListener {

    AllWaysOnTop() {
        // Code to setup your frame
        addWindowListener(this);
        // Code to show your frame
    }

    // The window event handlers. We use WindowDeactivated to
    // try and keep the splash screen on top. Usually only keeps
    // the splash screen on top of our own java windows.
    public void windowOpened(WindowEvent event){};
    public void windowActivated(WindowEvent event){};
    public void windowDeactivated(WindowEvent event){
        toFront();
    }
    public void windowIconified(WindowEvent event){};
    public void windowDeiconified(WindowEvent event){};
    public void windowClosed(WindowEvent event){};
    public void windowClosing(WindowEvent event) {};
}

Reference This forum post

Tasawer Khan
  • 5,994
  • 7
  • 46
  • 69
  • When a new application will start in full screen mode. it should deactivate this frame. And on deactivation we bring it to front. Should work technically. let me try. – Tasawer Khan Jun 16 '10 at 18:09
  • Then the full-screen app will get kicked out and minimize itself no? I think the OP is looking for a way to have his windows displayed in front of a full-screen app. – Ben S Jun 16 '10 at 18:10
  • Yes you are right. Layne did not specificly mentioned this but it will behave like you said. – Tasawer Khan Jun 16 '10 at 18:13
  • Ben your right, the fullscreen window should be still visible and not minimized. –  Jun 16 '10 at 18:13
0

I know this post is old, but I encountered this problem and I found a satisfying solution. My program has some notifications that I wish to be always on top, but when a movie entered full-screen, they disappeared. Fortunate, my program updates these notifications every 5 seconds, and if I call setVisible(true) on these JWindows, at each update, they regain the top position, if they have lost it.

bear
  • 81
  • 5
0

If you mean fullscreen as in DirectX/OpenGL/whatever, I'm not sure you can (or should) really pull it off. Most operating systems disable their native windowing during full screen to improve rendering performance. Swing works via the native windowing toolkit.

You could write something that uses a timer and in short intervals (e.g., 200ms) instructs your window to go to the top. Depending on your operating system this would be exactly what you need, or a horrible cause of performance trouble or flickering.

Uri
  • 88,451
  • 51
  • 221
  • 321
0

I'm not sure but i would bet that the Fullscreen window also has Always On Top set to true, and in that case you have stumbled into the realm of undefined behavior. In general when two windows are set to always on top there is no guarantee about ordering. I think in general though the order is just dependent on the order in which they were set to always on top. So in that case i would just wait until the app has gone fullscreen to set it to always on top and see if that works.

In other cases ive seen people start threads and then occasionally reset the fram to always on top.

All these solutions are ugly, so just use the one that lets you sleep at night.

luke
  • 14,518
  • 4
  • 46
  • 57
-1

I was looking to do the same thing as OP, have my app running in the foreground while my game ran. It doesn't work in fullscreen but if you put the game into windowed mode and adjust the window settings to fit your tv it works. I only needed the frame.setAlwaysOnTop to make it work.

JonnyDoeInWisco
  • 233
  • 1
  • 4
  • 12