tags:

views:

85

answers:

6

Hey, 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.

A: 

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

Danny Chen
+1  A: 

This sounds like the kind of question that Raymond Chen always has to answer over at http://blogs.msdn.com/b/oldnewthing/. 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?

Jim Kiley
A: 

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
Aye, the timer idea would result in horrible flickering for sure!
mcandre
+1  A: 

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
+1  A: 

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

Taz
This won't work if another app goes full-screen.
Ben S
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.
Taz
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
Yes you are right. Layne did not specificly mentioned this but it will behave like you said.
Taz
Ben your right, the fullscreen window should be still visible and not minimized.
Layne
This does not work as desired :(
Taz
A: 

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