Forum Controls
Spotlight Features

The Rich Engineering Heritage Behind Dependency Injection

Andrew McVeigh takes us on a tour of the rich heritage behind dependency injection, what it represents, and tells us why its here to stay.
Replies: 7 - Pages: 1  
Threads: [ Previous | Next ]
  Click to reply to this thread Reply

SWT: Tray Icons and Tooltips

At 11:03 PM on Mar 16, 2006, R.J. Lorimer Javalobby Editors 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.TrayIcon class.

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.org
Author              - http://www.coffee-bytes.com
Software Consultant - http://www.numbersix.com

  Click to reply to this thread Reply
1. At 8:46 AM on Mar 21, 2006, Naiden Gochev Occasional Javalobby Visitor wrote:

Re: SWT: Tray Icons and Tooltips

hm that looks very nice ;) I have not used SWT until know but it looks very nice
  Click to reply to this thread Reply
2. At 12:32 AM on Apr 25, 2006, Hans Raber Javalobby Newcomers wrote:

Re: SWT: Tray Icons and Tooltips

has anyone tested if it works on os x?
  Click to reply to this thread Reply
3. At 10:32 AM on Jun 23, 2006, Nate Snapp Javalobby Newcomers wrote:

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!
  Click to reply to this thread Reply
4. At 5:46 AM on Jun 16, 2007, Amgad Hanafy Occasional Javalobby Visitor wrote:

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
Amgad Hanafy mailto:amgoody2002@yahoo.com
  Click to reply to this thread Reply
5. At 10:12 AM on Jul 24, 2007, maciej Javalobby Newcomers wrote:

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 ??
  Click to reply to this thread Reply
6. At 6:58 AM on Nov 20, 2007, Toby Weston Blooming Javalobby Member wrote:

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
  Click to reply to this thread Reply
7. At 4:04 PM on Sep 10, 2008, Toby Weston Blooming Javalobby Member wrote:

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?

thread.rss_message