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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 | 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" ); } } } |