Can i create "Always on top" funciton in JAVA?
Hi, my name is Chang Hee Lee and I am a programmer in South Korea. We are developing a messenger program in JAVA. I have been trying to create a tool like MSN messenger's "Always on top" function under Tools in the menu bar.
I could not find any way to develope the same function in JAVA. I would appreciate it if you could help me solve this problem. If you have any idea how to enable "Always on top" finction with JAVA, please send me an e-mail toc-hee@honmail.net Thank you.
[508 byte] By [
kostrich] at [2007-9-26 23:27:22]

I am interested in teh answer, too. Please post it here.
JNI (this is really the wrong forum, but hey):
The following is butchered code from all over the place.
**** windowAlwaysOnTop.c ****
#include <jni.h>
#include "Frame1.h"
#include <stdio.h>
#include<windows.h>
JNIEXPORT void JNICALL Java_Frame1_WindowAlwaysOnTop(JNIEnv *env, jclass obj, jint hwnd, jboolean flag)
{
if (flag)
SetWindowPos((HWND) hwnd,HWND_TOPMOST,0,0,0,0,SWP_NOMOVE|SWP_NOSIZE);
else
SetWindowPos((HWND) hwnd,HWND_NOTOPMOST,0,0,0,0,SWP_NOMOVE|SWP_NOSIZE);
return;
}
**** Frame1.java ****
import java.awt.*;
import sun.awt.*;
import sun.awt.windows.*;
import javax.swing.*;
import java.awt.event.*;
public class Frame1 extends JFrame {
static boolean onTopSupported = true;
static {
try{
System.loadLibrary("windowOnTop");
} catch(Throwable t) {
onTopSupported = false;
}
}
int windowHWND = 0;
JButton jButton1 = new JButton();
public Frame1() {
try {
sun.awt.SunToolkit tk = (sun.awt.SunToolkit) Toolkit.getDefaultToolkit();
windowHWND = tk.getNativeWindowHandleFromComponent( this ) ;
} catch (Throwable t) {
onTopSupported = false;
}
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
private native void WindowAlwaysOnTop(int hwnd, boolean flag);
public void setOnTop( boolean onTop ){
if(onTopSupported) {
System.out.println("Working");
sun.awt.SunToolkit tk = (sun.awt.SunToolkit) Toolkit.getDefaultToolkit();
windowHWND = tk.getNativeWindowHandleFromComponent( this ) ;
WindowAlwaysOnTop( windowHWND, onTop );
} else {
System.out.println("Hi");
}
}
public static void main(String[] args) {
Frame1 frame11 = new Frame1();
frame11.setSize(400,400);
frame11.setVisible(true);
}
private void jbInit() throws Exception {
jButton1.setText("jButton1");
this.addWindowListener(new java.awt.event.WindowAdapter() {
public void windowOpened(WindowEvent e) {
this_windowOpened(e);
}
public void windowClosing(WindowEvent e) {
this_windowClosing(e);
}
});
this.getContentPane().add(jButton1, BorderLayout.NORTH);
}
boolean ont = false;
void this_windowOpened(WindowEvent e) {
ont =!ont;
setOnTop(ont);
}
void this_windowClosing(WindowEvent e) {
System.exit(0);
}
}
********
Use javah Frame1 to create the header "Frame1.h".
Use fav. C/C++ compiler to create lib named "winwindowOnTop.dll", put in libpath, run.
mike
mlk at 2007-7-4 12:55:55 >

Thanks you guys, I tried your code and finished my program, so i'd like to share with you my experiance:
if you want to use JKD 1.4, where is no more methods to get the HWND of a window, you can use the window's title instead like this:
(NOTE: you may also want to hide/show the window's title bar, so you i included the following additional 2 functions, too, for your convience. enjoy:-)
<code>
//set the specified window(given its title) on top most
JNIEXPORT void JNICALL Java_kle_KleWin32_WindowAlwaysOnTopStr
(JNIEnv *env, jobject obj, jstring title, jboolean flag)
{
char buf[128];
const char *str = env->GetStringUTFChars(title, 0);
strcpy(buf, str);
(env)->ReleaseStringUTFChars(title, str);
HWND hwnd;
hwnd=::FindWindow(NULL,buf);
if (flag)
SetWindowPos(hwnd,HWND_TOPMOST,0,0,0,0,SWP_NOMOVE|SWP_NOSIZE);
else
SetWindowPos(hwnd,HWND_NOTOPMOST,0,0,0,0,SWP_NOMOVE|SWP_NOSIZE);
return;
}
// hide/show the title bar of a specified window
JNIEXPORT void JNICALL Java_kle_KleWin32_SetWindowTitleBar
(JNIEnv *env, jobject obj, jstring title, jboolean flag)
{
char buf[128];
const char *str = env->GetStringUTFChars(title, 0);
strcpy(buf, str);
(env)->ReleaseStringUTFChars(title, str);
HWND hwnd;
hwnd=::FindWindow(NULL,buf);
if(hwnd==NULL)
{
MessageBox(NULL,"Window Not Found!!!","DLL Run time Error",MB_OK|MB_ICONERROR);
return ;
}
if (flag)
{
LONG style=GetWindowLong(hwnd,GWL_STYLE);
style |= WS_POPUP | WS_THICKFRAME | WS_CAPTION;
::SetWindowLong(hwnd,GWL_STYLE,style);
::SetWindowLong(hwnd,GWL_EXSTYLE,WS_EX_STATICEDGE|WS_EX_TOOLWINDOW);
}
else
{
LONG style=GetWindowLong(hwnd,GWL_STYLE);
style |= WS_POPUP | WS_THICKFRAME;
style &= ~WS_CAPTION;
::SetWindowLong(hwnd,GWL_STYLE,style);
::SetWindowLong(hwnd,GWL_EXSTYLE, WS_EX_STATICEDGE|WS_EX_TOOLWINDOW);
}
//remember to resize the window to force a repaint
return;
}
</code>
Hi, seawan,
I have some problem in compling the c file, the error message below:
Borland C++ 5.5.1 for Win32 Copyright (c) 1993, 2000 Borland
Frame1.c:
Error E2288 Frame1.c 9: Pointer to structure required on left side of -> or ->* in function Java_Frame1_WindowAlwaysOnTop
Error E2288 Frame1.c 11: Pointer to structure required on left side of -> or ->* in function Java_Frame1_WindowAlwaysOnTop
Error E2140 Frame1.c 13: Declaration is not allowed here in function Java_Frame1_WindowAlwaysOnTop
Warning W8057 Frame1.c 21: Parameter 'env' is never used in function Java_Frame1_WindowAlwaysOnTop
Warning W8057 Frame1.c 21: Parameter 'obj' is never used in function Java_Frame1_WindowAlwaysOnTop
Warning W8057 Frame1.c 21: Parameter 'title' is never used in function Java_Frame1_WindowAlwaysOnTop
*** 3 errors in Compile ***
I am using 1.4.1 right now and the old technique will not work on getting the window handle. Please give out some hint on how to handle it.
Thanks.
Fan Luo
just figure out: need to the save the file as .cpp as c++ file. After that, everything works fine. Great!
HI, I had the same problem with the borland compiler spitting out E2288 for examples from the JNI tutorial. Here's what solved it for me:
The Borland Compiler (Bcc32) has different compilation modes. By default it chooses the mode based on the extension of your source file:
bcc32 myfile.c- this will compile in C mode
bcc32 myfile.cpp- this will compile in C++ mode
(you can force it to do C++ compilation regardless of file extension by typing:
bcc32 -P myfile.c)
When the compiler is in C++ mode, it doesn't allow
jstring jstr = (*env)->GetObjectArrayElement(env, arr, i);
and spits out the error E2288 . Instead you have to change this to:
jstring jstr = env->GetObjectArrayElement(arr, i);
this goes for other statements too, such as:
const char *str = env->GetStringUTFChars(prompt, 0);
Hi dark_venikI had a similar prb. thanks for ur comments.
How can I have my JWindow always on top in Linux. I am using Redhat Linux 9.
As far as I'm concerned, I'm using a JWindow on Linux with kde3.3, jdk1.5, and it appears always on top, and is displayed on all desktops... I did not take any special step to obtain this "effect", and it actually bothers me quite a lot (debugging a program with a big "always on top" window in the middle of the screen is a real nightmare).
Does anybody have the same problem? Does anybody have a solution?
Hi dark_venik,I had the same problem, thanks!
does any one tried same example on linux redhat 9 ?
How to develope AppBar in Linux
Can some one help me?I have a window developed in JAVA .I want to make it as AppBar in Linux. Can anyOne give me linux code that accepts window handle and makes it AppBar?I have done it on Windows and it's working there.
Can any 1 help me?How to get handle of any running application on linux platform through JNI ?
Hi Guys,
No need to mess up with the coding any more as with jdk1.5, sun has added the function "setAlwaysOnTop". This can be reached in the jdk1.5 documentation with the Window documentation or JWindow. Similarly all child classes and JDialog can also use this function to be on top.
Can i create "AppBar" in LINUX?Plz read this topic that i have posted and give me the suggestion if u have?Thank U.
Hi All, Is it possible to draw a rectangle on windows Desktop through java.Plz give me the source code.
I am not using jdk 1.5.Here I am using only 1.4.So How to use setAlwaysOnTop() in my code.Is theer any other way .. to solve this one?if sooo .. plz let me know !
is the followingJFrame.setAlwaysOnTop(true) ;too easy ?