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?
After you create another window, call toFront() on your JFrame that you want to be at the front. myFrame. setAlwaysOnTop(true);
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..
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 ); } }
Window remains on top even when is not active
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With