Introduction
Hi Guys, This is my third article in Codeproject. I hava already posted
SsTab Control in Java
and Java Chat Application with great customizable GUI. I hope this
will also useful for all java developers.
This is Simple Java System Tray Application.. This Works
only in Windows Environment. No Need For JNI.
Description
Here, I am using VB Api for putting java application in
System Tray.
This is Vb Code For System Tray
Private Declare Function Shell_NotifyIcon Lib "shell32.dll" Alias
"Shell_NotifyIconA" (ByVal dwMessage As Long, lpData As NOTIFYICONDATA) As Long
Private Const WM_MOUSEMOVE = &H200
Private Const NIF_ICON = &H2
Private Const NIF_MESSAGE = &H1
Private Const NIF_TIP = &H4
Private Const NIM_ADD = &H0
Private Const NIM_MODIFY = &H1
Private Const NIM_DELETE = &H2
Private Const MAX_TOOLTIP As Integer = 64
Private Type NOTIFYICONDATA
cbSize As Long
hwnd As Long
uID As Long
uFlags As Long
uCallbackMessage As Long
hIcon As Long
szTip As String * MAX_TOOLTIP
End Type
Private nfIconData As NOTIFYICONDATA
.....
This is the Vb code for Killing System Tray Application.
Private Const WM_CLOSE = &H10
Private Declare Function PostMessage Lib "user32" Alias "PostMessageA"
(ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Private Declare Function FindWindow Lib "user32" Alias "
FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Sub Form_Load()
PostMessage FindWindow(vbNullString, "SystemTray"), WM_CLOSE, ByVal 0, ByVal 0
End
End Sub
This is Vb Api for Hiding window from the task.
Private Declare Function ShowWindow Lib "user32"
(ByVal hwnd As Long, ByVal nCmdShow As Long) As Long
Private Declare Function FindWindow Lib "user32" Alias
"FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Const SW_HIDE = 0
Private Sub Form_Load()
WindowHandle = FindWindow(vbNullString, "SystemTray In Java")
ShowWindow WindowHandle, SW_HIDE
End
End Sub
SO, after finishing these all VB part, we have to make exe files.
afer that, using process and runtime class,
we have to call these exe's.
The Main Step in this System Tray Application is "Window Title"
Here, i am using "SystemTray In Java" as a Window Title.
In Out Vb Program, we have to use this window tiltle for
Finding ,hiding,and showing window.
Here, this is the Java Code for System Tray Application.
try {
process = Runtime.getRuntime().exec("data/SystemTrayKill.exe");
}catch(IOException _IoExc) { }
dispose();
System.exit(0);
}
});
setLayout(new BorderLayout());
setSize(400, 200);
setTitle("SystemTray In Java");
Label LblTray = new Label("System Tray Demo in Java !",1);
LblTray.setBackground(Color.black);
LblTray.setForeground(Color.white);
LblTray.setFont(new Font("Arial",1,25));
add("North",LblTray);
CmdTray = new Button("Go To System Tray");
CmdTray.addActionListener(this);
add("Center",CmdTray);
Label LblDevelop = new Label("Developed By Jeeva S (vavjeeva@yahoo.com)",1);
LblDevelop.setBackground(Color.black);
LblDevelop.setForeground(Color.white);
LblDevelop.setFont(new Font("Arial",1,15));
add("South",LblDevelop);
try {
process = Runtime.getRuntime().exec("data/SystemTray.exe");
}catch(IOException _IoExc) { }
show();
}
public void actionPerformed(ActionEvent evt)
{
if (evt.getSource().equals(CmdTray))
{
try {
process = Runtime.getRuntime().exec("data/SystemTrayHide.exe");
}catch(IOException _IoExc) { }
}
}
}
Conclusion
I hope this wil help u guys.. If you have any doupts contact me
My mail id is
vavjeeva@yahoo.com
Have A Nice Day.
Jeeva S