SystemTray
Introduction
Nous allons créer un SystemTray avec un menu pour quitter l'application.
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 MySystemTray {
private Tray tray;
private TrayItem trayItem;
private Image trayImage;
private static Menu trayMenu;
private final String TRAY_IMAGE_PATH ="src/image/app.gif";
private final String TRAY_TOOLTIP_TEXT ="Tray Application";
public MySystemTray(Display display)
{
this.tray = display.getSystemTray();
this.trayItem = new TrayItem(tray, 0);
this.trayImage = new Image(display, TRAY_IMAGE_PATH);
this.trayItem.setToolTipText(TRAY_TOOLTIP_TEXT);
this.trayItem.setImage(this.trayImage);
// The tray item can only receive Selection/DefaultSelection (left click) or
// MenuDetect (right click) events
this.trayItem.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 trayMenu = new Menu(s, SWT.POP_UP);
// Creates a new menu item that terminates the program
// when selected
MenuItem exit = new MenuItem(trayMenu, SWT.NONE);
exit.setText("Quit");
exit.addListener(SWT.Selection, new Listener()
{
public void handleEvent(Event event)
{
System.exit(0);
}
});
// We need to make the menu visible
trayMenu.setVisible(true);
};
});
}
}