Showing a JFrame without taking away the focus from the current window

You will have to use JNI. The following is an example on how to do this. The JFrame contains one button. If you click it, it will be iconified for 2 seconds and restored again, but without taking away the focus from the window that currently has the focus.

I used JDK1.3.1 and cygwin to create the DLL.

1. Main.java:

import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
import java.util.*; 
 
import sun.awt.windows.*;
import sun.awt.*;
 
public class Main extends JFrame implements ActionListener
{
   public static native void WindowsShowNoActivate(int hwnd);
 
   static {
      System.loadLibrary("windowNoActivate");
   }
 
   public Main()
   {
      addWindowListener(new WindowAdapter() {
         public void windowClosing(WindowEvent we) {
            System.exit(1);
         }
      });
 
      JButton iconButton = new JButton("Iconify this Frame");
      getContentPane().setLayout(new FlowLayout(FlowLayout.LEFT));
      getContentPane().add(iconButton);
      iconButton.addActionListener(this);
   }
 
   public void actionPerformed(ActionEvent ae) {     
      this.setState(Frame.ICONIFIED);  
      try {
         Thread.sleep(2000);
      }
      catch(Exception e) { }
 
      showWindowNoActivate();
      //this.setState(Frame.NORMAL);  
      //this.toBack();
   }
 
   public int getHwnd() {
      DrawingSurfaceInfo w = (DrawingSurfaceInfo) 
        ((DrawingSurface) getPeer()).getDrawingSurfaceInfo();
      w.lock();
      WDrawingSurfaceInfo win32 = (WDrawingSurfaceInfo) w;
      int hwnd = win32.getHWnd();
      w.unlock();
     
      return hwnd;
   }
 
   public void showWindowNoActivate() {
     WindowsShowNoActivate(this.getHwnd());
   }
 
   public static void main(String args[])
   {
      Main main = new Main();
      main.setSize(300, 150);
      main.setVisible(true);
   }
}

2. Compile and run javah:

c:\> javac Main.java
c:\> javah Main

3. Create windowNoActivate.c:

#include "jni.h"
#include "Main.h"
#include <stdio.h>
#include <windows.h>

JNIEXPORT void JNICALL Java_Main_WindowsShowNoActivate(JNIEnv *env, jclass obj,
jint hwnd)
{
   SetWindowPos((HWND) hwnd, HWND_TOPMOST, 0,0,0,0, SWP_NOMOVE|SWP_NOSIZE);
   ShowWindow((HWND) hwnd, SW_SHOWNOACTIVATE);
}

4. I used Cygwin to create the DLL. There is a change that needs to be made to JDK/include/win32/jni_md.h. Add the following line:

typedef long long __int64;

5. Compile windowNoActivate.c:

$ gcc -L -DDBG -shared windowNoActivate.c -mno-cygwin 
      -I/usr/include -Ic:/jdk1.3.1/include 
      -Ic:/jdk1.3.1/include/win32 -Wl,--add-stdcall-alias -o windowNoActivate.dll

6. Put the DLL in the same directory as where your run Main from or in your path, and run it.