There is a JFrame window that should always be in focus. How can this be achieved? Such things as:

this.setFocusable(true); this.requestFocus(); this.toFront(); 

Do not make the window active if it has lost activity. If you can’t do it with Java tools, then the WinAPI option would also work.

In C #, this task is solved with the help of:

 this.Activate(); this.TopLevel = true; this.TopMost = true; 

    3 answers 3

    JNA + WinAPI will help you, I will give an example of how to make a window on top of all windows. In this example, I'm working with the running Windows Calculator application. You can get the window handle in any other way convenient for you.

     package test.jna; import com.sun.jna.Native; import com.sun.jna.platform.win32.User32; import com.sun.jna.platform.win32.WinDef.HWND; import com.sun.jna.win32.W32APIOptions; public class testjna { public interface MyUser32 extends User32 { static final MyUser32 instance=(MyUser32) Native.loadLibrary("user32", MyUser32.class, W32APIOptions.DEFAULT_OPTIONS); public boolean SetWindowPos(HWND hwnd, int hwnd2, int arg1, int arg2, int arg3, int arg4, int flags); public int EnableWindow(HWND hwnd, boolean enabled); } public static void main(String[] params){ HWND tst= MyUser32.instance.FindWindow(null, "Калькулятор"); MyUser32.instance.SetWindowPos(tst, -1, 200, 200, 100, 100, 0x0040|0x0002|0x0001); MyUser32.instance.EnableWindow(tst, true); } } 

    If you need a modal window from which you cannot go back (well, for example, as a file selection or a dialog box), then you need to set all parents of this window to enabled to false (there is no such option for the window "modality", but if it has all parents not active, you cannot switch out of it).
    To change the activity is the function EnableWindow.

    • Thank!!! Everything works perfectly! Now I understand how to work with Java from WinAPI. - dmail1976
      frame.setAlwaysOnTop( true ); frame.setLocationByPlatform( true ); 
    • Thank!!! I will know about the existence of these methods. But the result is not the same as using WinAPI. I needed to make a congratulatory Windows blocker - so that the window would immediately overlap, called the task manager. Through WinAPI this can be achieved, but through these two methods is not, although in C # these tools were enough. - dmail1976

    The listener hangs on the desired frame. As soon as it loses focus, it makes it active and on top of all windows. Thus all the time it switches. Accordingly, closing the frame with a cross or button - does not produce the effect of returning to the window, since it is closed.

     // Minimize frame window
         public class iconifiedListener implements WindowListener {
             @Override
             public void windowActivated (WindowEvent e) {
                  System.out.println ("windowActivated");
             }
    
             @Override
             public void windowClosed (WindowEvent e) {
                  System.out.println ("windowClosed");
             }
    
             @Override
             public void windowClosing (WindowEvent e) {
                  System.out.println ("windowClosing");
             }
    
             @Override
             public void windowDeactivated (WindowEvent e) {
                  frame.setFocusable (true);
                  frame.toFront ();
             }
    
             @Override
             public void windowDeiconified (WindowEvent e) {
                  System.out.println ("windowDeiconified");
             }
    
             @Override
             public void windowIconified (WindowEvent e) {
                  System.out.println ("windowIconified");
    
             }
    
             @Override
             public void windowOpened (WindowEvent e) {
                  System.out.println ("windowOpened");
             }
         }