Notice
Recent Posts
Recent Comments
Link
«   2024/03   »
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31
Archives
Today
Total
03-29 01:54
관리 메뉴

zyint's blog

System Tray Icon 본문

예전글들

System Tray Icon

진트­ 2007. 3. 1. 23:14

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);
}

출처 : http://www.eclipsezone.com/eclipse/forums/t66093.rhtml
Comments