Jan 27, 2008

How to add TrayItem in Eclipse RCP application?

In an application like Yahoo Messenger or Skype, you don't want to quit the application when the main window is closed. Most of these applications will move themselves to System Tray when the main window is closed and clicking on the Tray icon would bring back the window. Lets try to see how to do it in an Eclipse RCP Application.

When the window is closed, the WorkbenchWindowAdvisor.preWindowShellClose() method is called. If this returns false, then the close request will not be honored. So all we need to do is to override this method; create a TrayItem; hide the window and return false.


trayItem = new TrayItem(Display.getDefault().getSystemTray(), SWT.NONE);
trayItem.setText("My RCP App");
trayItem.setImage(getAppIcon());
trayItem.addSelectionListener(getSelectionListener());
getWindowConfigurer().getWindow().getShell().setVisible(false);
return false;

In the SelectionListener, we can add code to bring up the window:

Shell workbenchWindowShell = getWindowConfigurer().getWindow().getShell();
workbenchWindowShell.setVisible(true);
workbenchWindowShell.setActive();
workbenchWindowShell.setFocus();
workbenchWindowShell.setMinimized(false);
trayItem.dispose();


Simple :-)

0 comments: