I am developing an OSX application using SWT and I have a TrayIcon in the system tray. I got it to work, but there is this one little thing that really bugs me. When you right click the icon, the context menu is displayed just how I intended.
(right click)
When I left click, however, it just makes the menu pop-up.
(left click)
How can I make the left click behave like the right?
The code for both are the pretty much the same:
item.addListener (SWT.Selection, new Listener () {
public void handleEvent (Event event) {
menu.setVisible(true);
}
});
item.addListener (SWT.MenuDetect, new Listener () {
public void handleEvent (Event event) {
menu.setVisible (true);
}
});
In case anyone wants to see the code for the whole project:
import org.eclipse.swt.SWT;
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;
import org.eclipse.wb.swt.SWTResourceManager;
public class Icon {
public static void main(String[] args) {
Display display = new Display ();
Shell shell = new Shell (display);
shell.setVisible(false);
final Tray tray = display.getSystemTray ();
if (tray == null) {
System.out.println ("The system tray is not available");
} else {
final TrayItem item = new TrayItem (tray, SWT.NONE);
item.setToolTipText("Test Tool Tip");
final Menu menu = new Menu (shell, SWT.POP_UP);
MenuItem mi = new MenuItem (menu, SWT.PUSH);
mi.setText ("View Messages");
mi.addListener (SWT.Selection, new Listener () {
public void handleEvent (Event event) {
System.out.println("Messages will be displayed.");
}
});
new MenuItem(menu, SWT.SEPARATOR);
MenuItem mi1 = new MenuItem (menu, SWT.PUSH);
mi1.setText ("Help");
mi1.addListener (SWT.Selection, new Listener () {
public void handleEvent (Event event) {
System.out.println("Help will be displayed.");
}
});
MenuItem mi2 = new MenuItem (menu, SWT.PUSH);
mi2.setText ("Version 1.0");
item.addListener (SWT.DefaultSelection, new Listener () {
public void handleEvent (Event event) {
menu.setVisible (true);
}
});
item.addListener (SWT.Selection, new Listener () {
public void handleEvent (Event event) {
menu.setVisible(true);
}
});
item.addListener (SWT.MenuDetect, new Listener () {
public void handleEvent (Event event) {
menu.setVisible (true);
}
});
item.setImage(SWTResourceManager.getImage(Icon.class, "/main/black.png"));
}
while (!shell.isDisposed ()) {
if (!display.readAndDispatch ()) display.sleep ();
}
display.dispose ();
}
}