On running the application an icon will appear in the system tray. On left clicking on the icon a menu-submenu will appear. On left clicking on the icon anEXIT option will be shown. Clicking on the exit will remove the icon from system tray.
package systemtray; import java.awt.SystemTray; import org.eclipse.swt.SWT; import org.eclipse.swt.graphics.Image; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Event; import org.eclipse.swt.widgets.Listener; import org.eclipse.swt.widgets.Menu; import org.eclipse.swt.widgets.MenuItem; import org.eclipse.swt.widgets.Shell; import org.eclipse.swt.widgets.Tray; import org.eclipse.swt.widgets.TrayItem; public class SysTrayTest { /** * * @param args */ public static void main(String[] args) { if (SystemTray.isSupported()) { // We will need this final Display display = Display.getDefault(); // Retrieves the system tray singleton final Tray tray = display.getSystemTray(); // Creates a new tray item (displayed as an icon) final TrayItem item = new TrayItem(tray, 0); item.setToolTipText("Tray Icon in action"); item.setImage( new Image(display, "images.jpg")); // MenuDetect (right click) events item.addListener(SWT.MenuDetect, new Listener() { public void handleEvent(org.eclipse.swt.widgets.Event event) { // We need a Shell as the parent of our menu Shell s = new Shell(event.display); // Style must be pop up Menu m = new Menu(s, SWT.POP_UP); // Creates a new menu item that terminates the program // when selected MenuItem exit = new MenuItem(m, SWT.None); exit.setText("Exit!"); exit.addListener(SWT.Selection, new Listener() { public void handleEvent(Event event) { item.dispose(); display.dispose(); System.exit(0); } }); // We need to make the menu visible m.setVisible(true); }; }); //Left click (Selection) events item.addListener(SWT.Selection, new Listener() { public void handleEvent(org.eclipse.swt.widgets.Event event) { // We need a Shell as the parent of our menu final Shell s = new Shell(event.display); // Style must be pop up final Menu m = new Menu(s, SWT.POP_UP); // Creates a new menu item. Add menu items to increase number of options // when selected MenuItem menu01 = new MenuItem(m, SWT.None); menu01.setText("Menu01! "); final MenuItem menu02 = new MenuItem(m, SWT.CASCADE); menu02.setText("Menu02! "); //submenu starts final Menu subMenu = new Menu(s, SWT.DROP_DOWN); //creates submenu items final MenuItem menu02Child01 = new MenuItem(subMenu, SWT.PUSH); menu02Child01.setText("menu02Child01"); final MenuItem menu02Child02 = new MenuItem(subMenu, SWT.PUSH); menu02Child02.setText("menu02Child02"); final MenuItem menu02Child03 = new MenuItem(subMenu, SWT.PUSH); menu02Child03.setText("menu02Child03"); //adding subMenu to menu02 menu02.setMenu(subMenu); MenuItem menu03 = new MenuItem(m, SWT.POP_UP); menu03.setText("Menu03! "); MenuItem leftClick = new MenuItem(m, SWT.None); leftClick.setText("LeftClick! Right Click to exit."); // We need to make the menu visible m.setVisible(true); }; }); // Wait forever... while (true) { if (!display.readAndDispatch()) display.sleep(); } }else { System.out.println("Sorry your system doesnot support TrayIcon"); } } }