"Always on Top" Windows with Java
Asked Answered
J

3

96

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?

Johannesburg answered 18/11, 2008 at 5:37 Comment(0)
L
179

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

Leninakan answered 18/11, 2008 at 5:46 Comment(13)
you would think a simple search for "java application always on top" on would have this answer, but it couldn't find it. Thanks.Johannesburg
Guess what. Now it does!! :) It brings you here! google.com/search?&q=java+application+always+on+topLeninakan
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.Swot
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?Culinary
@Dream lane I'd probably ask that as a new questionAugean
This works for a login dialog box but my main application window then goes to the background. I'm open to suggestions.Dowski
This is like the first code I've seen anywhere ever that pads out boolean parameters with spaces.Nadanadab
@Nadanadab 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.Leninakan
This works great but if the user switches to different desktop/screen in mac or linux how to make it show in that screen alsoAdrianaadriane
Does not work if Google Chrome DevTools are opened: #58372267Cruickshank
@Cruickshank hm yes it does i.stack.imgur.com/H99Ra.png (11yrs later, that code still runs)Leninakan
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.Cohbert
this answer is spam. OP answer meant to have a aplication window from already opened windows. not to create a window in javaAudiogenic
B
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();}
    }
Burchett answered 4/1, 2014 at 16:15 Comment(3)
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?Conroy
@Trejkaz Well, that's a valid point. I'm no longer in touch with swing now.Burchett
This works for my use-case perfectly! Setting it back to false first was key.Coarse
C
-1

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

Collocation answered 22/7, 2018 at 3:11 Comment(2)
Is it an answer or comment?Broadminded
@FailedScientist a glorified commentUnderbody

© 2022 - 2024 — McMap. All rights reserved.