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]
# 1
I am interested in teh answer, too. Please post it here.
FatihC at 2007-7-4 12:55:55 > top of Java-index,Other Topics,Java Community Process (JCP) Program...
# 2

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 > top of Java-index,Other Topics,Java Community Process (JCP) Program...
# 3

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>

seawan at 2007-7-4 12:55:55 > top of Java-index,Other Topics,Java Community Process (JCP) Program...
# 4

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

fanluohaikou at 2007-7-4 12:55:55 > top of Java-index,Other Topics,Java Community Process (JCP) Program...
# 5
just figure out: need to the save the file as .cpp as c++ file. After that, everything works fine. Great!
fanluohaikou at 2007-7-4 12:55:55 > top of Java-index,Other Topics,Java Community Process (JCP) Program...
# 6

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);

dark_venik at 2007-7-4 12:55:55 > top of Java-index,Other Topics,Java Community Process (JCP) Program...
# 7
Hi dark_venikI had a similar prb. thanks for ur comments.
j2ee_freek at 2007-7-4 12:55:55 > top of Java-index,Other Topics,Java Community Process (JCP) Program...
# 8
How can I have my JWindow always on top in Linux. I am using Redhat Linux 9.
javabb at 2007-7-4 12:55:55 > top of Java-index,Other Topics,Java Community Process (JCP) Program...
# 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?

GuillaumePothier at 2007-7-4 12:55:55 > top of Java-index,Other Topics,Java Community Process (JCP) Program...
# 10
Hi dark_venik,I had the same problem, thanks!
cosminj at 2007-7-4 12:55:55 > top of Java-index,Other Topics,Java Community Process (JCP) Program...
# 11
does any one tried same example on linux redhat 9 ?
varale at 2007-7-4 12:55:55 > top of Java-index,Other Topics,Java Community Process (JCP) Program...
# 12
How to develope AppBar in Linux
Sandeep.Nair at 2007-7-4 12:55:55 > top of Java-index,Other Topics,Java Community Process (JCP) Program...
# 13
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.
Sandeep.Nair at 2007-7-4 12:55:55 > top of Java-index,Other Topics,Java Community Process (JCP) Program...
# 14
Can any 1 help me?How to get handle of any running application on linux platform through JNI ?
Sandeep.Nair at 2007-7-4 12:55:55 > top of Java-index,Other Topics,Java Community Process (JCP) Program...
# 15

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.

brucesword at 2007-7-4 12:55:57 > top of Java-index,Other Topics,Java Community Process (JCP) Program...
# 16
Can i create "AppBar" in LINUX?Plz read this topic that i have posted and give me the suggestion if u have?Thank U.
Sandeep.Nair at 2007-7-4 12:55:57 > top of Java-index,Other Topics,Java Community Process (JCP) Program...
# 17
Hi All, Is it possible to draw a rectangle on windows Desktop through java.Plz give me the source code.
Sandeep.Nair at 2007-7-4 12:55:57 > top of Java-index,Other Topics,Java Community Process (JCP) Program...
# 18
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 !
ashok_111 at 2007-7-4 12:55:57 > top of Java-index,Other Topics,Java Community Process (JCP) Program...
# 19
is the followingJFrame.setAlwaysOnTop(true) ;too easy ?
PenguinMan6 at 2007-7-4 12:55:57 > top of Java-index,Other Topics,Java Community Process (JCP) Program...