95

In Java, is there a way to have a window that is "Always on top" regardless if the user switches focus to another application? I've searched the web, and all of the solutions lean to some sort of JNI interface with native bindings. Truly this can't be the only way to do it?.. or is it?

OscarRyz
  • 196,001
  • 113
  • 385
  • 569
Laplie Anderson
  • 6,345
  • 4
  • 33
  • 37

3 Answers3

177

Try this method of the Window class:

Window.setAlwaysOnTop(boolean)

It works the same way as the default in the Windows TaskManager: switch to another app but it shows always on top.

This was added in Java 1.5

Sample code:

import javax.swing.JFrame;
import javax.swing.JLabel;

public class Annoying {
    public static void main(String[] args) {
        JFrame frame = new JFrame("Hello!!");

        // Set's the window to be "always on top"
        frame.setAlwaysOnTop( true );

        frame.setLocationByPlatform( true );
        frame.add( new JLabel("  Isn't this annoying?") );
        frame.pack();
        frame.setVisible( true );
    }
}

alt text

Window remains on top even when is not active

tstec
  • 313
  • 5
  • 9
OscarRyz
  • 196,001
  • 113
  • 385
  • 569
  • 4
    you would think a simple search for "java application always on top" on would have this answer, but it couldn't find it. Thanks. – Laplie Anderson Nov 18 '08 at 14:21
  • 50
    Guess what. Now it does!! :) It brings you here! http://www.google.com/search?&q=java+application+always+on+top – OscarRyz Oct 15 '09 at 00:07
  • This is simple and awesome. I was also looking for something like this, but didn't know they implemented this in Java 1.5. Thanks for posting. – Kushal Paudyal Nov 18 '09 at 22:43
  • 2
    Unfortunately this does not work for me when running a full screen application such as a video game. Anyone know of a way to force it to the top in that situation? – Dream Lane May 05 '11 at 21:02
  • 1
    @Dream lane I'd probably ask that as a new question – rogerdpack May 13 '11 at 13:27
  • This works for a login dialog box but my main application window then goes to the background. I'm open to suggestions. – James P. May 01 '12 at 17:11
  • This is like the first code I've seen anywhere ever that pads out boolean parameters with spaces. – MMJZ Apr 11 '14 at 15:05
  • @MMJZ haha true. Actually I'm surprised I didn't do it for the `args` declaration. I **always** use space after parenthesis. This is a very very old habit that I develop back in the days I used notepad to program. Double clicking on a word would select the parenthesis if there is no space between, which was very annoying because I had to delete them afterwards if what I intended to do was re-use the variable somewhere else. – OscarRyz Apr 11 '14 at 21:31
  • This works great but if the user switches to different desktop/screen in mac or linux how to make it show in that screen also – jan_kiran May 30 '18 at 15:06
  • Does not work if Google Chrome DevTools are opened: https://stackoverflow.com/questions/58371767/how-to-show-java-filedialog-in-front-of-google-dev-tools-alwaysontop-does-not-w – Stefan Oct 14 '19 at 07:21
  • @Stefan hm yes it does https://i.stack.imgur.com/H99Ra.png (11yrs later, that code still runs) – OscarRyz Oct 15 '19 at 13:48
  • lol, "Hello!! Isn't this annoying?" Indeed it is VERY annoying when a window obstructs my entire desktop (and other apps during alt-tab). `setAlwaysOnTop` should be rarely used for very extreme cases (like the task manager or other system-wide window) or if the user specifically requested it. – MasterHD Apr 02 '21 at 13:27
  • this answer is spam. OP answer meant to have a aplication window from already opened windows. not to create a window in java – vincent thorpe Mar 09 '23 at 09:30
11

From my observation I found that AlwaysOnTop privilege is given to the latest process which requested to be always on top.

So, if you have an application which setAlwaysOnTop(true) and later another application uses this option, the privilege is given to the second application. In order to work around this I have set the setAlwaysOnTop(false) and again setAlwaysOnTop(true) whenever any window comes on top of the current window.

I've checked it with wordweb in windows. WordWeb is one of the applications which uses AlwaysOnTop option from the OS

I'm not sure about if it works properly with your game scenario.

Warning: I'm not aware of the side effects.

Here is the code example:

import java.awt.event.*;

import javax.swing.*;

public class MainWindow extends JFrame implements WindowFocusListener
{
    public MainWindow()
    {
        addWindowFocusListener(this);
        setAlwaysOnTop(true);
        this.setFocusable(true);
       // this.setFocusableWindowState(true);
        panel = new JPanel();
        //setSize(WIDTH,HEIGHT);
        setUndecorated(true);
        setLocation(X,Y);
        setExtendedState(MAXIMIZED_BOTH);
        setVisible(true);
    }

    public void windowGainedFocus(WindowEvent e){}
    public void windowLostFocus(WindowEvent e)
    {
        if(e.getNewState()!=e.WINDOW_CLOSED){
            //toFront();
            //requestFocus();
            setAlwaysOnTop(false);
            setAlwaysOnTop(true);
            //requestFocusInWindow();
            System.out.println("focus lost");
        }

    }

    private JPanel panel;
    private static final int WIDTH = 200;
    private static final int HEIGHT = 200;
    private static final int X = 100;
    private static final int Y = 100;

    public static void main(String args[]){
              new MainWindow();}
    }
pinkpanther
  • 4,770
  • 2
  • 38
  • 62
  • 1
    Setting it to false and true again when another window comes on top doesn't sound like a workable solution for one simple reason - what if the other application does the same thing? – Hakanai Apr 13 '15 at 03:36
  • @Trejkaz Well, that's a valid point. I'm no longer in touch with swing now. – pinkpanther Apr 13 '15 at 07:39
  • This works for my use-case perfectly! Setting it back to false first was key. – rococo Nov 20 '17 at 23:53
-1

dont use setFullScreenWindow,just get the screen size and then setSize, and everything will be fine.