Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Always on Top" Windows with Java

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?

like image 913
Laplie Anderson Avatar asked Nov 18 '08 05:11

Laplie Anderson


People also ask

How do you make 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);

What is top level window in Java?

A 'top-level window' or 'top level container' is something that can be shown on screen without having to add it to another component. We would start a GUI with a top level container, and then add panels and components to that TLC. E.G. of top level containers..


1 Answers

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

like image 134
OscarRyz Avatar answered Nov 15 '22 14:11

OscarRyz