Say it sys-tray instead of taskbar
here it goes....
A Java programmer often misses the ease with which a VB or Visual C++ application can integrate with the Windows desktop. This is because Java, at its core, being platform independent tries to provide the most common UI components that are expected to be available across various OSs.
Steps to system tray integration
We assume that you are well versed with developing GUI applications with Java Swing. To install a Java application on the system tray, all you need to do is:
* Construct a JPopupMenu (Java Swing)
* Construct one or more JMenuItems and add them to the JPopupMenu (Java Swing)
* Create an ImageIcon (Java Swing)
* Construct a TrayIcon with the JPopupMenu as a pa rameter (JDIC specific)
* Get a reference (a SystemTray object) to the Windows System Tray (JDIC specific)
* Add the TrayIcon to the SystemTray (JDIC specific)
The code for the process is as follows.
// step 1
JPopupMenu menu = new JPopupMenu("Menu");
// step 2
JMenuItem menuItem1 = new JMenuItem("Menu 1");
menu.add(menuItem1);
// repeat step 2 to add more menu items to the JPopupMenu
//step 3
ImageIcon icon = new ImageIcon("icon.jpg");
// step 4
TrayIcon trayIcon = new TrayIcon(icon, "Hello System Tray", menu);
// step 5
SystemTray tray = SystemTray.getDefaultSystemTray( );
// step 6
tray.addTrayIcon(trayIcon);
Note that the parameters to the TrayIcon( ) constructor are the ImageIcon object, tooltip (that will show when you move the mouse over the tray icon) and the JPopupMenu respectively.
Following is the code for a ready to compile and run Java program, which will install on the system tray.
import javax.swing.*;
import org.jdesktop.jdic.tray.*;
import java.awt.event.*;
public class SystemTrayDemo extends JFrame{
public SystemTrayDemo(){
JPopupMenu menu = new JPopupMenu("Menu");
JMenuItem menuItem1 = new JMenuItem("Menu1");
menu.add(menuItem1);
JMenuItem menuItem2 = new JMenuItem("Menu2");
menu.add(menuItem2);
JMenuItem menuItem3 = new JMenuItem("Menu3");
menu.add(menuItem3);
JMenuItem menuItem4 = new JMenuItem("Exit");
menu.add(menuItem4);
menuItem4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
System.exit(0);
}});
ImageIcon icon = new ImageIcon("icon.jpg");
TrayIcon trayIcon = new TrayIcon(icon, "Hello System Tray", menu);
SystemTray tray = SystemTray.getDefaultSystemTray();
tray.addTrayIcon(trayIcon);
}
public static void main(String[] args){
try {
javax.swing.UIManager.setLookAndFeel("com.sun.java.swing. plaf.windows.WindowsLookAndFeel");
}
catch(Exception e) {
System.out.println(e);
}
new SystemTrayDemo();
}}
Save the code as SystemTrayDemo.java in a directory, systray (say). Create/download any JPEG file in this directory (to display the tray icon) and call it icon.jpg.
Compile and run
Download the J2SE SDK 5.0 Update 1 for Windows from www.java.sun.com. Go to https://jdic.dev.java.net/servlets/ProjectDocumentList and download the JDIC package. Click on jdic-0.8.8 and download the file named jdic-0.8.8-bin-windows.zip. Unzip the archive and copy files named jdic.dll, tray.dll and jdic.jar to the systray directory. Compile the code and execute it as:
javac -classpath jdic.jar;. SystemTrayDemo.java
java -cp jdic.jar;. SystemTrayDemo
You should be able to see a tray icon on the system tray. Right click on it to see the popup menu with menu items-Menu1, Menu2, Menu3 and Exit.
To close or exit, click on Exit to call the System.exit( ) method-specified in the ActionListener for menuItem4 object.
Hope this will help
Cheers:)