SWT SYSTEM TRAY can only handle three events: Left click, left click, right click, right-click this is what I see SWT 3.0.1 Win32 source code, this DEMO has the following functions: 1. Left button When you click, hide, display the main window 2. Right-click, pop-up menu Note: 1. Left button Double-click and right click to double-click an event, no distinguish 2. Left button will produce the following event: Left click, left Key Double click, left click on 3. About the pop-up menu, Menu must have a Parent, if there is no shell in the program, you can build an invisible shell as a Menu's Parent.
Import org.eclipse.swt. *;
Import org.eclipse.swt.widgets. *;
Import org.eclipse.swt.events. *;
Import org.eclipse.swt.layout.FillLayout;
Import org.eclipse.swt.graphics.image;
Class HelloTray Implements SelectionListener {
Display display;
Shell shell;
Button Hello;
TRAY TRAY;
TRAYITEM TI;
Menu Menu;
Menuitem Mi1;
Menuitem MI2;
Menuitem quit;
Public hellotray () {
Display = display.getDefault ();
Shell = new shell ();
FillLayout FillLayout = New FillLayout ();
FillLayout.Type = SWT.VERTICAL;
Shell.setLayout (FillLayout);
Hello = New Button (shell, swt.none);
Hello.setText ("Hello World!");
Hello.addSelectionListener (New SelectionAdapter () {
Public void widgetselected (SelectionEvent E) {
IF (Hello.get (). Equals ("Hello World!"))
Hello.setText ("Clicked");
Else
Hello.setText ("Hello World!");
}
});
Menu = new menu (shell);
Mi1 = New Menuitem (Menu, SWT.PUSH);
Mi1.Settext ("MenuItem 1");
Mi1.addseelectrionListener (this);
Mi2 = New Menuitem (Menu, SWT.PUSH);
Mi2.Settext ("Menuitem 2");
Mi2.addseelectrionListener (this);
Quit = New Menuitem (Menu, SWT.PUSH);
Quit.Settext ("quit");
Quit.AddseLectionListener (this);
/ / Generate SWT TRAY
TRAY = display.getsystemtray ();
Ti = new trayitem (tray, 0);
Ti.SetTooltiptext ("this is a swt tray!");
Ti.setImage (new image (display, "e: // my documents // my picture // alm.gif"));
// SWT, TRAY all events:
Ti.AddSelectionListener (New SelectionListener () {
// Left click
Public void widgetSelected (SelectionEvent E) {system.out.println ("TRAY SELCTED");
// Hide, display the main window when you click
IF (shell.isvisible ()) {
Shell.setVisible (FALSE);
} else {
Shell.setVisible (TRUE);
Shell.ForceActive ();
}
}
// Left button double click, right-click double click, it is it
Public void widgetdefaultselected (SelectionEvent E) {
System.out.println ("TRAY WIDGETDEFAULECTED");
}
});
/ / Right-click, pop-up menu
Ti.Addlistener (SWT.MENUDETECT, New Listener () {
Public void Handleevent (Event Event) {
System.out.println ("SWT.MENUDETECT");
Menu.setlocation (Display.getCursorLocation ());
Menu.setVisible (TRUE);
}
});
}
Public void start () {
shell.open ();
While (! shell.isdisposed ()) {
IF (! display.readDispatch ())
Display.sleep ();
}
Display.dispose ();
}
Public static void main (String [] args) {
Hellotray app = new hellotray ();
app.start ();
}
Public void widgetselected (SelectionEvent E) {
System.out.println (E.GetSource () "SELECTED.");
IF (E.getsource () == quit) {
System.out.println ("quit");
Display.dispose ();
System.exit (0);
}
}
Public void widgetdefaultselected (SelectionEvent E) {
}
}