Many operating systems have the concept of a 'system tray', and it's often nice to be able to ofter a 'tray icon' feature for your application. SWT ships with tray icon support in the form of the
org.eclipse.swt.widgets.TrayIcon
class.
The null check is required simply because not all platforms have a tray, and as such it's an optional feature.
Here is a screenshot of the tray icon on my Ubuntu Linux installation:
Most tray items have some kind of menu associated with them. Adding a menu is also quite straightforward.
Tray tray = display.getSystemTray();
if(tray != null) {
TrayItem item = new TrayItem(tray, SWT.NONE);
item.setImage(image);final Menu menu = new Menu(shell, SWT.POP_UP);
MenuItem menuItem = new MenuItem(menu, SWT.PUSH);
menuItem.setText("Button A");
menuItem = new MenuItem(menu, SWT.PUSH);
menuItem.setText("Button B");
menuItem = new MenuItem(menu, SWT.PUSH);
menuItem.setText("Show Tooltip");
item.addListener (SWT.MenuDetect, new Listener () {
publicvoid handleEvent (Event event) {
menu.setVisible (true);
}
});
}
Recent releases of SWT (post-M5) finally support balloons/tooltips through the tray icon. Balloons are quite common in platforms with tray icons, so this is a nice feature to finally have. To add a tooltip, simply use the
TrayItem.setToolTip(ToolTip)
method, and set the visibility of the tooltip to true when you want to show it.
final ToolTip tip = new ToolTip(shell, SWT.BALLOON | SWT.ICON_INFORMATION);
tip.setMessage("Balloon Message Goes Here!");
Tray tray = display.getSystemTray();
if (tray != null) {
TrayItem item = new TrayItem(tray, SWT.NONE);
item.setImage(image);
tip.setText("Balloon Title goes here.");
item.setToolTip(tip);
final Menu menu = new Menu(shell, SWT.POP_UP);
MenuItem menuItem = new MenuItem(menu, SWT.PUSH);
menuItem.setText("Button A");
menuItem = new MenuItem(menu, SWT.PUSH);
menuItem.setText("Button B");
menuItem = new MenuItem(menu, SWT.PUSH);
menuItem.setText("Show Tooltip");
// Add tooltip visibility to menu item.
menuItem.addListener (SWT.Selection, new Listener () {
publicvoid handleEvent (Event e) {
tip.setVisible(true);
}
});
// Add menu detection listener to tray icon.
item.addListener (SWT.MenuDetect, new Listener () {
publicvoid handleEvent (Event event) {
menu.setVisible (true);
}
});
}
One of the suggestions made in the SWT snippets is to still show the tooltip even if tray support isn't available (typically, tooltips are triggered by some application behavior). To do this, the tooltip has to be placed manually, rather than as a relative of the tray icon.
final ToolTip tip = new ToolTip(shell, SWT.BALLOON | SWT.ICON_INFORMATION);
tip.setMessage("Balloon Message Goes Here!");
tip.setText("Balloon Title goes here.");
Tray tray = display.getSystemTray();
if (tray != null) {
TrayItem item = new TrayItem(tray, SWT.NONE);
item.setImage(image);
item.setToolTip(tip);
final Menu menu = new Menu(shell, SWT.POP_UP);
MenuItem menuItem = new MenuItem(menu, SWT.PUSH);
menuItem.setText("Button A");
menuItem = new MenuItem(menu, SWT.PUSH);
menuItem.setText("Button B");
menuItem = new MenuItem(menu, SWT.PUSH);
menuItem.setText("Show Tooltip");
menuItem.addListener (SWT.Selection, new Listener () {
publicvoid handleEvent (Event e) {
tip.setVisible(true);
}
});
item.addListener (SWT.MenuDetect, new Listener () {
publicvoid handleEvent (Event event) {
menu.setVisible (true);
}
});
}
else {
// Set the tooltip location manually.
tip.setLocation(100, 100);
}
I tried this on Mac OSX Tiger, and unfortunately it doesn't work. Display.getSystemTray() returns null. I have a work around that I like quite a bit. There is an open source notification system for Mac OSX called Growl. The work around depends on that being installed. But once it is, in the else statement you can simply put:
Growl notifier;
notifier = new Growl("AppName", new com.apple.cocoa.foundation.NSData());
String[] tmpNotif = {"AppNotification"};
notifier.setAllowedNotifications(tmpNotif);
notifier.register();
How to create a sliding window or message box near the system tray like the one for msn messenger or yaho messenger notification for new message or user login
I tried to run this code wriiten above with ballon-shaped tooltips and on linux evrything's fine but on windows ballons just don't appear when I call ToolTip.setVisible method. Does any one have the same problem ??
SWT: Tray Icons and Tooltips
At 11:03 PM on Mar 16, 2006, R.J. Lorimer
wrote:
Many operating systems have the concept of a 'system tray', and it's often nice to be able to ofter a 'tray icon' feature for your application. SWT ships with tray icon support in the form of the
org.eclipse.swt.widgets.TrayIconclass.Using a tray icon is fairly simple in SWT:
image = new Image(display, BalloonExample.class.getResourceAsStream("tray_icon.gif")); // ... Tray tray = display.getSystemTray(); if(tray != null) { TrayItem trayItem = new TrayItem(tray, SWT.NONE); trayItem.setImage(image); }The null check is required simply because not all platforms have a tray, and as such it's an optional feature.
Here is a screenshot of the tray icon on my Ubuntu Linux installation:
Most tray items have some kind of menu associated with them. Adding a menu is also quite straightforward.
Tray tray = display.getSystemTray(); if(tray != null) { TrayItem item = new TrayItem(tray, SWT.NONE); item.setImage(image);final Menu menu = new Menu(shell, SWT.POP_UP); MenuItem menuItem = new MenuItem(menu, SWT.PUSH); menuItem.setText("Button A"); menuItem = new MenuItem(menu, SWT.PUSH); menuItem.setText("Button B"); menuItem = new MenuItem(menu, SWT.PUSH); menuItem.setText("Show Tooltip"); item.addListener (SWT.MenuDetect, new Listener () { public void handleEvent (Event event) { menu.setVisible (true); } }); }Recent releases of SWT (post-M5) finally support balloons/tooltips through the tray icon. Balloons are quite common in platforms with tray icons, so this is a nice feature to finally have. To add a tooltip, simply use the
TrayItem.setToolTip(ToolTip)method, and set the visibility of the tooltip to true when you want to show it.final ToolTip tip = new ToolTip(shell, SWT.BALLOON | SWT.ICON_INFORMATION); tip.setMessage("Balloon Message Goes Here!"); Tray tray = display.getSystemTray(); if (tray != null) { TrayItem item = new TrayItem(tray, SWT.NONE); item.setImage(image); tip.setText("Balloon Title goes here."); item.setToolTip(tip); final Menu menu = new Menu(shell, SWT.POP_UP); MenuItem menuItem = new MenuItem(menu, SWT.PUSH); menuItem.setText("Button A"); menuItem = new MenuItem(menu, SWT.PUSH); menuItem.setText("Button B"); menuItem = new MenuItem(menu, SWT.PUSH); menuItem.setText("Show Tooltip"); // Add tooltip visibility to menu item. menuItem.addListener (SWT.Selection, new Listener () { public void handleEvent (Event e) { tip.setVisible(true); } }); // Add menu detection listener to tray icon. item.addListener (SWT.MenuDetect, new Listener () { public void handleEvent (Event event) { menu.setVisible (true); } }); }One of the suggestions made in the SWT snippets is to still show the tooltip even if tray support isn't available (typically, tooltips are triggered by some application behavior). To do this, the tooltip has to be placed manually, rather than as a relative of the tray icon.
final ToolTip tip = new ToolTip(shell, SWT.BALLOON | SWT.ICON_INFORMATION); tip.setMessage("Balloon Message Goes Here!"); tip.setText("Balloon Title goes here."); Tray tray = display.getSystemTray(); if (tray != null) { TrayItem item = new TrayItem(tray, SWT.NONE); item.setImage(image); item.setToolTip(tip); final Menu menu = new Menu(shell, SWT.POP_UP); MenuItem menuItem = new MenuItem(menu, SWT.PUSH); menuItem.setText("Button A"); menuItem = new MenuItem(menu, SWT.PUSH); menuItem.setText("Button B"); menuItem = new MenuItem(menu, SWT.PUSH); menuItem.setText("Show Tooltip"); menuItem.addListener (SWT.Selection, new Listener () { public void handleEvent (Event e) { tip.setVisible(true); } }); item.addListener (SWT.MenuDetect, new Listener () { public void handleEvent (Event event) { menu.setVisible (true); } }); } else { // Set the tooltip location manually. tip.setLocation(100, 100); }Until next time,
R.J. Lorimer
Contributing Editor -rj -at- javalobby.orgAuthor -http://www.coffee-bytes.comSoftware Consultant -http://www.numbersix.com7 replies so far (
Post your own)
Re: SWT: Tray Icons and Tooltips
hm that looks very niceRe: SWT: Tray Icons and Tooltips
has anyone tested if it works on os x?Re: SWT: Tray Icons and Tooltips
I tried this on Mac OSX Tiger, and unfortunately it doesn't work. Display.getSystemTray() returns null. I have a work around that I like quite a bit. There is an open source notification system for Mac OSX called Growl. The work around depends on that being installed. But once it is, in the else statement you can simply put:Growl notifier;
notifier = new Growl("AppName", new com.apple.cocoa.foundation.NSData());
String[] tmpNotif = {"AppNotification"};
notifier.setAllowedNotifications(tmpNotif);
notifier.register();
try{
notifier.notifyGrowlOf("AppNotification", "Message Title", "My Cool Growl Message");
}
catch(Exception e){}
// and Viola!
Re: SWT: Tray Icons and Tooltips
That's really great,But I have another question
How to create a sliding window or message box near the system tray like the one for msn messenger or yaho messenger notification for new message or user login
Re: SWT: Tray Icons and Tooltips
Hi,I tried to run this code wriiten above with ballon-shaped tooltips and on linux evrything's fine but on windows ballons just don't appear when I call ToolTip.setVisible method. Does any one have the same problem ??
Re: SWT: Tray Icons and Tooltips
Great tip for Growl, thanks.Any ideas where com.apple.cocoa.foundation.NSData comes from? Having trouble locating a JAR.
Cheers
Re: SWT: Tray Icons and Tooltips
Any tips for SWT 3.4 which now supports Mac? I can get icons on the system tray (great) but have problems causing a popup (a balloon tool tip)...anyone managed it?