"Always on Top" Windows with Java

Always on Top Windows with Java

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

JWindow always on top

SOLUTION

I add a MouseAdapteras a MouseListener to PointWindow and when mousePressed event is detected then do toFront() and work fine but there is one side effect, it means that there is a moment when window is hide and show (very fast).

How to set window always on top of any other applications

It depends on the operating system. In general, you can't guarantee that your window will always be on top when it comes to native windows, nor should you. The task manager is always-on-top for good reasons.

I remember that some versions of Vista and older Windows systems allowed that and the native and Java windows ended up fighting for focus.

ALWAYS on top window

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.

How to set a window always on top of another specific window in javafx

When you create the second Stage you have to call initOwner and initModality with Modality.WINDOW_MODAL. Then the new stage is always on top of the other but you can't interact with the parent stage.

For example:

public void createNewStage(Window parent) {
//... all the other stuff
Stage onTop = new Stage();
onTop.initOwner(parent);
onTop.initModality(Modality.WINDOW_MODAL);
onTop.show();
}

Keeping Window OnTop of all other windows C/Java

Short answer: Not really, no.

Longer answer: A "full-screen" app uses the display in a different way than a normal Windows GUI app. Exactly how they work depends on the application and the OS; most will leverage the DirectX or OpenGL APIs which allow applications to directly manipulate the memory representing the screen, as well as access the accelerated capabilities of the GPU. These apps are supported by basically creating a GUI "window" (more like a panel) that covers the entire primary display and is "always on top", preventing the desktop elements from ever being painted as they're always behind the full-screen app's window in the Z-order. The application then gets "relaxed" access to the memory representing the display rectangle of that GUI window, so it can manipulate individual pixels without needing to use the message loop to repaint that area.

In this situation, the display is being painted when the application wants (which is virtually always either "as fast as possible" or "synchronized with the next monitor scan"), NOT when the Windows GUI thinks it's a good time. So, anything that IS painted when Windows thinks it's a good idea will flicker; Windows paints your "always on top" window over the app's "window" in the GUI's Z-order, then the app paints back over the window by drawing directly onto its rectangle. That causes Windows to invalidate and redraw your window, and the cycle continues.

The solution is not only to make an "always-on-top" window, but to somehow programmatically "task-switch" from the full-screen app's window to yours. This may require your app to have privileges that most managed runtimes can't or won't grant. If it's possible, then when it happens the full-screen app will minimize (which may or may not be a HUGE problem for your users; whatever your app is trying to tell me, it will almost certainly NOT be worth minimizing my StarCraft 2 session in the middle of an online melee).

JavaFX setting window Always on top with JNA lib, windows os

Don't use JNA, use Java8u20 or any other subsequent version, which has a JavaFX always on top feature.

stage.setAlwaysOnTop(true);

There is a nice blog post by Carl Dea on always on top.

Frame always on top of my program only

Ok, I found a solution (don't know if it is THE solution, but it's working, so...)

I discovered setFocusableWindowState(), that is perfect for toolbars. By the way, I don't know if my previous setFocusable(false) had any effect.

The next issue was that the focus gets very weird behaviour with this code : If I switch from MyApp to Firefox, here is what happens :

focus : MyApp -> Firefox
execution of MyDialog.toFront()
focus : Firefox -> MyDialog
MyDialog not focusable !
focus : MyDialog -> MyApp !!!

result : nothing changed !

So I finally got the tricks : just after MyDialog.toFront(), you give back the focus to the previous owner. And the only way I found to do this with no error was : mainFrame.toBack()

Final code :

public class Test {
private static JFrame mainFrame;

public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
mainFrame = new JFrame("test");
mainFrame.setSize(800,600);
mainFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
mainFrame.setVisible(true);

A a = new A();
}
});
}

public static class A extends JDialog {

public A() {
super(mainFrame);
setAlwaysOnTop(true);
setFocusableWindowState(false);
setSize(80,60);
setVisible(true);

mainFrame.addWindowListener(new WindowAdapter() {
@Override
public void windowActivated(WindowEvent e) {
A.this.setAlwaysOnTop(true);
A.this.toFront();
}
@Override
public void windowDeactivated(WindowEvent e) {
A.this.setAlwaysOnTop(false);
}
});
}
}
}


Related Topics



Leave a reply



Submit