Shell keeps poping up when creating a menu tray
843785Feb 14 2009 — edited Feb 16 2009Hey,
I would like to create an application that creates a system tray and on base of the contents of the tray some actions are perfomed.
I have found the following code on the internet.
But if I run this the menu tray is created but also an empty screen is popping up (I think thats this part of the code shell.setBounds(50, 50, 300, 200); )
How can you create a menu tray without having the shell being pop-upped. (so only a menu tray is being created)
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
//Test3 application = new Test3();
//application.initialize();
//application.setVisible(true);
Display display = new Display ();
Shell shell = new Shell (display);
Image image = new Image (display, 16, 16);
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("SWT TrayItem");
item.addListener (SWT.Show, new Listener () {
public void handleEvent (Event event) {
System.out.println("show");
}
});
item.addListener (SWT.Hide, new Listener () {
public void handleEvent (Event event) {
System.out.println("hide");
}
});
final Menu mainMenu = new Menu (shell, SWT.POP_UP);
for (int i =0; i <= 3; i++){
MenuItem mi = new MenuItem(mainMenu, SWT.PUSH);
mi.setText("Main Menu Item " + i);
mi.setEnabled(false);
if (i == 3 ){
mi.setEnabled(true);
mi.setText("Exit program");
mi.addListener(SWT.Selection, new Listener (){
public void handleEvent (Event event){
System.exit(0);
}
});
}
}
item.addListener (SWT.Selection, new Listener () {
public void handleEvent (Event event) {
System.out.println("selection");
mainMenu.setVisible(true);
}
});
item.addListener (SWT.DefaultSelection, new Listener () {
public void handleEvent (Event event) {
System.out.println("default selection");
}
});
final Menu menu = new Menu (shell, SWT.POP_UP);
for (int i = 0; i < 8; i++) {
MenuItem mi = new MenuItem (menu, SWT.PUSH);
mi.setText ("Item" + i);
mi.addListener (SWT.Selection, new Listener () {
public void handleEvent (Event event) {
System.out.println("selection " + event.widget);
}
});
if (i == 0) menu.setDefaultItem(mi);
}
item.addListener (SWT.MenuDetect, new Listener () {
public void handleEvent (Event event) {
menu.setVisible (true);
}
});
item.setImage (image);
}
shell.setBounds(50, 50, 300, 200);
shell.open ();
while (!shell.isDisposed ()) {
if (!display.readAndDispatch ()) display.sleep ();
}
image.dispose ();
display.dispose ();
}